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

时间:2018-03-27 04:02:06

标签: android listview android-adapter

我的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;
  }

活动代码

 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);

  }

 });

1 个答案:

答案 0 :(得分:0)

Complete Demo for this

查看上面的链接以获取更多信息

1)遵循持有人模式

a)例如,您有4个视图,然后在持有者类中定义

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

b)在getView方法中,您将设置并获取此持有者类。

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)了解两个列表的使用

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

  • 您可以查看上面的链接,它有两个列表

//基于在编辑文本中输入的文本,它将更改,列表将根据此显示。 private List worldpopulationlist = null;

//此副本不会更改 private ArrayList arraylist;

b)了解过滤器的逻辑

// 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);
                }