This effect is actually to refresh the list data. Select the corresponding province to obtain the city data below it and refresh the list, select the corresponding city to obtain the district and county data below it and refresh the list, and return the selected data to the previous page; when the return or physical return key is triggered, do the same processing.
Implementation main code:
public class DataActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tvBack;
private ListView listview;
private ChoseDataAdapter choseDataAdapter;
private List<PlaceInfo> pList = new ArrayList<>();
private int cityIndex = 0;
private String prvStr, cityStr, townCity;//Province name city name district name
private String prvId, cityId, townId;//Province id City id District id
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_data);
initView();
}
private void initView() {
tvBack = (TextView) findViewById(R.id.tv_back);
tvBack.setOnClickListener(this);
ParseData.jsonToData(this);
//Province data
pList = PrioviceInfo.getProvice();
listview = (ListView) findViewById(R.id.listview);
listview.setDivider(null);
choseDataAdapter = new ChoseDataAdapter(DataActivity.this, pList);
listview.setAdapter(choseDataAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cityIndex++;
if (cityIndex == 1) {
PlaceInfo placeInfo = pList.get(position);
//Get the name and id of the selected province
prvStr = stringTOString(placeInfo.dict_name);
prvId = stringTOString(placeInfo.dict_id);
//Get the city / county corresponding to the selected province
pList = PrioviceInfo.getCity(prvId);
if (pList.size() == 0) {
placeInfo.valueName = prvStr;
placeInfo.id_1 = prvId;
getDataBack(placeInfo);
} else {
//Refresh city data and display
choseDataAdapter.nodfiyData(pList);
}
} else if (cityIndex == 2) {
PlaceInfo cityInfo = pList.get(position);
//Get the selected city name and id
cityStr = stringTOString(cityInfo.dict_name);
cityId = stringTOString(cityInfo.dict_id);
//Get the district / county data corresponding to the selected city
pList = PrioviceInfo.getTown(cityId);
if (pList.size() == 0) {
cityInfo.valueName = prvStr + " " + cityStr;
cityInfo.id_1 = prvId;
cityInfo.id_2 = cityId;
getDataBack(cityInfo);
} else {
//Refresh display district and county data
choseDataAdapter.nodfiyData(pList);
}
} else if (cityIndex == 3) {
PlaceInfo townInfo = pList.get(position);
townCity = stringTOString(townInfo.dict_name);
townId = stringTOString(townInfo.dict_id);
townInfo.valueName = prvStr + " " + cityStr + " " + townCity;
townInfo.id_1 = prvId;
townInfo.id_2 = cityId;
townInfo.id_3 = townId;
getDataBack(townInfo);
}
}
});
}
/**
* Go back to the previous page and return good data
*
* @param info
*/
private void getDataBack(PlaceInfo info) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("datainfo", info);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_back:
toBack();
break;
}
}
class ChoseDataAdapter extends DefaultAdapter<PlaceInfo> {
public ChoseDataAdapter(Context context, List<PlaceInfo> list) {
super(context, list);
}
@Override
protected BaseHolder<PlaceInfo> getHolder() {
return new ChoseDataHolder();
}
@Override
public void refreshUi(View convertView, int position) {
}
}
class ChoseDataHolder extends BaseHolder<PlaceInfo> {
TextView valueText;
@Override
public void refreshView(List<PlaceInfo> list, int pos) {
PlaceInfo sysBasicData = list.get(pos);
this.valueText.setText("" + sysBasicData.dict_name);
}
@Override
public View initView() {
View convertView = View.inflate(DataActivity.this, R.layout.chose_data_view, null);
this.valueText = findID(convertView, R.id.valueText);
return convertView;
}
}
private String stringTOString(String value) {
return value == null ? "" : value;
}
/**
* Return method
*/
private void toBack() {
cityIndex--;
if (cityIndex == 1) {
//Display the upper level city
pList = PrioviceInfo.getCity(prvId);
choseDataAdapter.nodfiyData(pList);
} else if (cityIndex== 0) {
pList=PrioviceInfo.getProvice();
choseDataAdapter.nodfiyData(pList);
} else if (cityIndex == -1) {
finish();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
toBack();
}
return true;
}
}
That's fine.
Source address:
https://pan.baidu.com/s/1boYvCAV