Android ListView 中的适配器搜索未更新

Adapter Search In Android ListView Not Getting Updated

我有一个 ListView,其中包含一个 TextViewCheckBox。我正在尝试过滤我的适配器。但是无法在ListView中得到更新的结果。 ListView 仅在搜索文本输入时清除。我在下面提供我的代码。感谢您花时间查看此问题。

适配器代码

 @Override
 public View getView(final int position, View convertView, final ViewGroup 
 parent) {
 LayoutInflater inflater = (LayoutInflater) context
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 final View rowView = 
 inflater.inflate(R.layout.activity_doctor_specialties_row, parent, false);
 TextView textView = (TextView) rowView.findViewById(R.id.lblSpeciality);
    final CheckBox mcheckbox = (CheckBox) 
 rowView.findViewById(R.id.cbSpeciality);
 textView.setText(values[position].getName());
 // Change the icon for Windows and iPhone
 String s = values[position].getName();
 if(dh.getSpltiesNameSelected()==null){
   String chkditems = dh.getSpltiesNameSelected().toString();
   if (chkditems.contains(s)) {
       mcheckbox.setChecked(true);
   }
  }

String selectedspecialty=dh.getSpltiesNameSelected().toString();
 if(selectedspecialty.contains(s)){ //for checking the returned data for 
    checked status
    mcheckbox.setChecked(true);
  }
  return rowView;
  }

Activity代码

 final EditText inputSearch = (EditText) findViewById(R.id.txtsearch);
  inputSearch.addTextChangedListener(new TextWatcher() {

  @Override
 public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
  adapter1.getFilter().filter(cs.toString().toLowerCase(Locale.US));

 }

  @Override
  public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                             int arg3) {

  }

  @Override
  public void afterTextChanged(Editable arg0) {

   adapter1.getFilter().filter(arg0);

  }

 });

Complete Demo for this

查看上面的 link 了解更多信息

1) 跟随持有人模式

a)比如你有4个view,那么在holder中定义class

public class ViewHolder {
        TextView rank;
        TextView country;
        TextView population;
        ImageView flag;
    }

b) 在 getView 方法中,您将设置并获取此支架 class.

public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listview_item, null);
            holder.rank = (TextView) view.findViewById(R.id.rank);
            holder.country = (TextView) view.findViewById(R.id.country);
            holder.population = (TextView) view.findViewById(R.id.population);
            holder.flag = (ImageView) view.findViewById(R.id.flag);
            view.setTag(holder);
//set for future use
        } else {
//get from previous object
            holder = (ViewHolder) view.getTag();
        }

有助于有效利用内存

2) 让我们来谈谈你的主要问题,

a) 理解两个列表的使用

  • 要过滤,您需要一个列表作为副本,另一个列表用于复制过滤后的值

  • 你可以查看上面的link,它有两个列表

//根据在编辑文本中输入的文本,它会发生变化,列表将以此为基础显示。 私有列表 worldpopulationlist = null;

//这个副本不会改变 私有 ArrayList 数组列表;

b) 理解filter的逻辑

// Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        worldpopulationlist.clear();
        if (charText.length() == 0) {
            worldpopulationlist.addAll(arraylist);
        } else {
            for (WorldPopulation wp : arraylist) {
                if (wp.getCountry().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    worldpopulationlist.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

在上面的逻辑中,它比较并添加到列表"worldpopulationlist", 你需要相应地改变逻辑

if (wp.getCountry().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    worldpopulationlist.add(wp);
                }