[英]Android listview can't be updated with getFilter() in android
I want to make searchable spinner and i use custom listview but when i use getFilter() the list can't be updated with filtered result although i debug the result and it gets right size of data我想制作可搜索的微调器,并且我使用自定义列表视图,但是当我使用 getFilter() 时,尽管我调试了结果并且它获得了正确的数据大小,但无法使用过滤结果更新列表
This is my code:这是我的代码:
Custom Adapter:自定义适配器:
public class BranchesSpinnerAdapter extends ArrayAdapter<BranchesSpinnerModel> {
List<BranchesSpinnerModel> branchesSpinnerLists = new ArrayList<>();
private ArrayList<HashMap<String, String>> filteredData;
Context context;
public BranchesSpinnerAdapter(Context context, int viewResourceId) {
super(context, viewResourceId);
this.context = context;
}
public void setList(List<BranchesSpinnerModel> branchesSpinnerLists) {
this.branchesSpinnerLists = branchesSpinnerLists;
notifyDataSetChanged();
}
public int getCount()
{
return filteredData.size();
}
public long getItemId(int position)
{
return position;
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
BranchesSpinnerItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.branches_spinner_item, parent, false);
BranchesSpinnerModel bs = branchesSpinnerLists.get(position);
binding.txTitle.setText(bs.getTitle());
return binding.getRoot();
}
// It gets a View that displays in the drop down popup the data at the specified position
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
// It gets a View that displays the data at the specified position
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults results = new FilterResults();
//If there's nothing to filter on, return the original data for your list
if(charSequence == null || charSequence.length() == 0)
{
results.values = branchesSpinnerLists;
results.count = branchesSpinnerLists.size();
}
else
{
ArrayList<String> filterResultsData = new ArrayList<>();
for(BranchesSpinnerModel data : branchesSpinnerLists)
{
//In this loop, you'll filter through originalData and compare each item to charSequence.
//If you find a match, add it to your new ArrayList
//I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
if(data.getTitle().contains(charSequence))
{
Log.e("charfilter", "true");
filterResultsData.add(data.getTitle());
Log.e("filterResultsData", String.valueOf(filterResultsData.size()));
}
}
results.values = filteredData;
results.count = filterResultsData.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
filteredData = (ArrayList<HashMap<String,String>>)filterResults.values;
notifyDataSetChanged();
}
};
}
}
My code in the fragment:我在片段中的代码:
branchesSpinnerAdapter = new BranchesSpinnerAdapter(getContext(), 0);
binding.branchesList.setAdapter(branchesSpinnerAdapter);
findCarViewModel.branchesSpinnerMutableLiveData.observe(getViewLifecycleOwner(), new Observer<List<BranchesSpinnerModel>>() {
@Override
public void onChanged(List<BranchesSpinnerModel> branchesSpinnerModel) {
branchesSpinnerAdapter.setList(branchesSpinnerModel);
branchesSpinnerAdapter.addAll(branchesSpinnerModel);
}
});
binding.receiptPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_spinner_searchable);
dialog.getWindow().setLayout(950, 1200);
dialog.show();
EditText edittext = dialog.findViewById(R.id.spinnerSearch);
ListView lv = dialog.findViewById(R.id.spinnerListview);
lv.setAdapter(branchesSpinnerAdapter);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
branchesSpinnerAdapter.getFilter().filter(charSequence);
branchesSpinnerAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
binding.receiptPlace.setText(branchesSpinnerAdapter.getItem(i).getTitle());
dialog.dismiss();
}
});
}
});
So how can i fix it to show filtered data when i type in search box那么,当我在搜索框中键入内容时,如何修复它以显示过滤后的数据
In the fragment, no need to have branchesSpinnerAdapter.notifyDataSetChanged();
在片段中,不需要有
branchesSpinnerAdapter.notifyDataSetChanged();
and try this adapter code:并尝试此适配器代码:
public class BranchesSpinnerAdapter extends ArrayAdapter<BranchesSpinnerModel> {
List<BranchesSpinnerModel> branchesSpinnerLists = new ArrayList<>();
private List<BranchesSpinnerModel> filteredData = new ArrayList<>(); // Changed
Context context;
public BranchesSpinnerAdapter(Context context, int viewResourceId) {
super(context, viewResourceId);
this.context = context;
}
public void setList(List<BranchesSpinnerModel> branchesSpinnerLists) {
this.branchesSpinnerLists = branchesSpinnerLists;
filteredData = new ArrayList<>(branchesSpinnerLists); // Added
notifyDataSetChanged();
}
public int getCount()
{
return filteredData.size();
}
public long getItemId(int position)
{
return position;
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
BranchesSpinnerItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.branches_spinner_item, parent, false);
// BranchesSpinnerModel bs = branchesSpinnerLists.get(position);
BranchesSpinnerModel bs = filteredData.get(position); // Changed
binding.txTitle.setText(bs.getTitle());
return binding.getRoot();
}
// It gets a View that displays in the drop down popup the data at the specified position
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
// It gets a View that displays the data at the specified position
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults results = new FilterResults();
//If there's nothing to filter on, return the original data for your list
if(charSequence == null || charSequence.length() == 0)
{
results.values = branchesSpinnerLists;
results.count = branchesSpinnerLists.size();
}
else
{
ArrayList<BranchesSpinnerModel> filterResultsData = new ArrayList<>();
for(BranchesSpinnerModel data : branchesSpinnerLists)
{
//In this loop, you'll filter through originalData and compare each item to charSequence.
//If you find a match, add it to your new ArrayList
//I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
if(data.getTitle().contains(charSequence))
{
Log.e("charfilter", "true");
filterResultsData.add(data); // Changed
Log.e("filterResultsData", String.valueOf(filterResultsData.size()));
}
}
results.values = filteredData;
results.count = filterResultsData.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
filteredData.clear(); // Added
filteredData.addAll((ArrayList<BranchesSpinnerModel>)filterResults.values); // Changed
notifyDataSetChanged();
}
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.