我为包含搜索视图和listview的自定义视图创建了通用ArrayList和通用数组适配器。对于此listview,我创建了通用ArrayList和通用arrayadapter,将仅用于更新数据并将其设置在listview上.i正在通过强制转换动态更新ArrayAdapter。但是当我通过notifydatasetchanged更新它时,它不会更新。在日志中,我正在获取所需的州和地区列表,但列表视图上没有更新。
public class activitySignUp extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, SearchView.OnQueryTextListener {
// Tag for logs...
private final String TAG = getClass().getName();
// Opens up Popup list for Selection...
private AppCompatTextView StateInput;
private AppCompatTextView DistrictInput;
// Popup view for Selection...
private AlertDialog builder;
// For Custom Views...
private ListView listView;
private SearchView searchView;
// Created Different Types Method and Adapters for different kinds of classes...
private static ArrayList < ? > GeneralList;
private static ArrayAdapter < ? > GeneralAdapter;
// This class will be used for storing View ID of the view, by clicking on we opened up the
// custom alert dialog. This class will be useful in Updating the Clicked UI.
private LastViewClicked lastViewClicked;
// For Storing Current values of selected items, will be helpful in dynamic function calls...
private static State Currentstate;
private static District Currentdistrict;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
initView();
}
private void initView() {
SignUpBar = findViewById(R.id.SignUpBar);
setSupportActionBar(SignUpBar);
StateInput = findViewById(R.id.stateInput);
DistrictInput = findViewById(R.id.districtInput);
// Inside context i am passing the context of the view according to switchview...
// intentRelatedTasks are for Passing intents...
// For showing error there's show Error...
builder = new AlertDialog.Builder(activitySignUp.this).create();
View custom_view = LayoutInflater.from(activitySignUp.this).inflate(R.layout.popup_view, null);
searchView = custom_view.findViewById(R.id.search_bar);
searchView.setIconifiedByDefault(false);
listView = custom_view.findViewById(R.id.list_all);
listView.setTextFilterEnabled(true);
builder.setView(custom_view);
// Initializing ArrayLists and Setting them up on Adapters...
GeneralList = new ArrayList();
GeneralAdapter = new ArrayAdapter < > (this, android.R.layout.simple_list_item_1, GeneralList);
listView.setOnItemClickListener(this);
searchView.setOnQueryTextListener(this);
// TextView on Click Listeners...
StateInput.setOnClickListener(this);
DistrictInput.setOnClickListener(this);
// Initializing class...
lastViewClicked = new LastViewClicked();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.stateInput:
// Storing the ID of the Last Clicked Item, which will be used in updating the UI on
// ListItem Click...
lastViewClicked.setView(StateInput);
getStates();
break;
case R.id.districtInput:
// Storing the ID of the Last Clicked Item, which will be used in updating the UI on
// ListItem Click...
lastViewClicked.setView(DistrictInput);
if (GeneralList.isEmpty()) {
Alerts.showToast(this, "Select State First...", Toast.LENGTH_SHORT);
} else {
if (Currentstate != null) {
Toast.makeText(this, "get Districts...", Toast.LENGTH_SHORT).show();
getDistrictsByStateId(Currentstate.getId());
}
}
break;
}
}
private void getStates() {
Api.getStates(new Callback < BasicResponse > () {
@Override
public void onResponse(Call < BasicResponse > call, Response < BasicResponse > response) {
if (response.isSuccessful() && response.isSuccessful()) {
BasicResponse basicResponse = response.body();
String res = basicResponse.getResponse();
Type listType = new TypeToken < ArrayList < State >> () {}.getType();
ShowList(GeneralList, GsonUtils.fromGson(res, listType), GeneralAdapter);
builder.show();
}
}
@Override
public void onFailure(Call < BasicResponse > call, Throwable t) {
Alerts.showToast(getApplication(), getResources().getString(R.string.NetworkError), Toast.LENGTH_SHORT);
Log.i(TAG, t.getMessage());
}
});
}
private void getDistrictsByStateId(String stateId) {
Api.getDistrictsByStateId(stateId, new Callback < BasicResponse > () {
@Override
public void onResponse(Call < BasicResponse > call, Response < BasicResponse > response) {
Log.i(TAG, "GetDistrictsByStateId");
Log.i(TAG, String.valueOf(response.isSuccessful()));
Log.i(TAG, GsonUtils.toGson(response.body()));
if (response.isSuccessful() && response.isSuccessful()) {
BasicResponse basicResponse = response.body();
String res = basicResponse.getResponse();
Type listType = new TypeToken < ArrayList < District >> () {}.getType();
ShowList(GeneralList, GsonUtils.fromGson(res, listType), GeneralAdapter);
builder.show();
}
}
@Override
public void onFailure(Call < BasicResponse > call, Throwable t) {
Log.i(TAG, t.getMessage());
}
});
}
private void ShowList(ArrayList arrayList, ArrayList new_list, ArrayAdapter arrayAdapter) {
arrayList.clear();
arrayAdapter.notifyDataSetChanged();
if (!new_list.isEmpty() && new_list != null) {
arrayList.addAll(new_list);
}
arrayAdapter.notifyDataSetChanged();
listView.setAdapter(arrayAdapter);
}
// Listview function that performs Action on OnItemClick...
@Override
public void onItemClick(AdapterView << ? > adapterView, View view, int position, long l) {
switch (lastViewClicked.getView().getId()) {
case R.id.stateInput:
Currentstate = (State) GeneralAdapter.getItem(position);
Log.d(TAG, "onItemClick: StateName" + Currentstate.getStateName());
break;
case R.id.districtInput:
// I am getting Error here stating that Adapter of State type cannot be cast to District Type....
// I am updating the adapter inside getStates Class and getDistrictbyState method but it's not updating...
Currentdistrict = (District) GeneralAdapter.getItem(position);
break;
}
searchView.setQuery("", false);
}
@Override
public boolean onQueryTextSubmit(String s) {
return true;
}
@Override
public boolean onQueryTextChange(String s) {
GeneralAdapter.getFilter().filter(s);
return true;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}