输入退格键时,ListView不更新后更新

时间:2014-06-27 08:09:01

标签: android listadapter

以下是代码,我使用了自定义适配器,并应用了Filter方法,现在当输入退格键时,ListView不会更新,当从项目活动切换回来时。请帮助我......以及如何在筛选列表中显示没有结果..

public class ItemListAdapter extends ArrayAdapter<ItemVO>
{

    private ArrayList<ItemVO> itemList;
    private ArrayList<ItemVO> fiems;
    private static final int LONG_DELAY = 3500; // 3.5 seconds
    private static final int SHORT_DELAY = 2000; // 2 seconds
    private Context context;
    private Filter filter;

    public ItemListAdapter(Context context, int textViewResourceId,ArrayList<ItemVO> stateList) 
    {
        super(context, textViewResourceId, stateList);
        this.context = context;
        this.itemList = new ArrayList<ItemVO>();
        this.itemList.addAll(stateList);
        this.fiems=new ArrayList<ItemVO>();
        this.fiems.addAll(stateList);

    }

    private class ViewHolder
    {
        TextView code;
        CheckBox name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null)
        {

            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.listview_items, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.textView1);
            holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);

            convertView.setTag(holder);

            holder.name.setOnClickListener( new View.OnClickListener() 
            {
                public void onClick(View v)  
                {
                    CheckBox cb = (CheckBox) v;
                    ItemVO item = (ItemVO) cb.getTag();

                    item.setSelected(cb.isChecked());
                }
            });

        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        ItemVO state = itemList.get(position);

        holder.code.setText(state.getItemdescription());
        holder.name.setChecked(state.isSelected());

        holder.name.setTag(state);

        return convertView;
    }
    @Override
    public Filter getFilter()
    {
        if (filter == null)
            filter = new PkmnNameFilter();

        return filter;
    }

    private class PkmnNameFilter extends Filter
    {
            @SuppressWarnings("unused")
            @SuppressLint("DefaultLocale")
            @Override
            protected FilterResults performFiltering(CharSequence constraint)
            {   
                FilterResults results = new FilterResults();  // Holds the results of a filtering operation in values
                String prefix = constraint.toString().toLowerCase();

                if (prefix == null || prefix.length() == 0)
                {
                    ArrayList<ItemVO> list = new ArrayList<ItemVO>(fiems);
                    results.values = list;
                    results.count = list.size();
                }
                else
                {
                    final ArrayList<ItemVO> list = new ArrayList<ItemVO>(fiems);
                    final ArrayList<ItemVO> nlist = new ArrayList<ItemVO>();
                    int count = list.size();

                    for (int i=0; i<count; i++)
                    {
                        final ItemVO pkmn = list.get(i);
                        final String value = pkmn.getItemdescription().toLowerCase();

                        if (value.startsWith(prefix))
                        {
                            nlist.add(pkmn);
                        }
                       /*else
                        {
                           final Toast toast= Toast.makeText(context, "No Items Found", Toast.LENGTH_SHORT);
                            toast.show();
                             Handler handler = new Handler();
                             handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    toast.cancel(); 
                                }
                         }, 1000);
                        }*/
                    }
                    // set the Filtered result to return
                    results.values = nlist;
                    results.count = nlist.size();

                }
                return results;
            }



            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                itemList = (ArrayList<ItemVO>)results.values; // has the filtered values

                clear();
                int count = itemList.size();
                for (int i=0; i<count; i++)
                {
                    ItemVO pkmn = (ItemVO)itemList.get(i);
                    add(pkmn);

                    notifyDataSetChanged(); // notifies the data with new filtered values
                }
            }

        }


    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        boolean notifyChanged = true;
    }
    }

主要活动 for editetxt ..

 dataAdapter = new ItemListAdapter(this,R.layout.listview_items, itemList);

                    lv.setAdapter(dataAdapter);
                    final EditText searchBox=(EditText) findViewById(R.id.filter_text);
                   //searchBox.addTextChangedListener(this);


                    lv.setTextFilterEnabled(true);





                 // Add Text Change Listener to EditText
                    searchBox.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                            // Call back the Adapter with current character to Filter
                            Log.d("Constants.TAG", "*** Search value changed: " + cs.toString());
                            CateringList.this.dataAdapter.getFilter().filter(cs);

                           dataAdapter.notifyDataSetChanged();

                        }

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

                        @Override
                        public void afterTextChanged(Editable arg0) {
                            //if(arg0.length()==0)
                           // {

                           // }
                        }
                    });

5 个答案:

答案 0 :(得分:1)

如果ArrayList将数据放在你的getView()方法

中,你只需要验证
if (itemList == null || itemList.size() == 0){
        //do your code here
      holder.code.setText("No Result");
    }

兄弟改变了这句话:

clear();
            int count = itemList.size();
            for (int i=0; i<count; i++)
            {
                ItemVO pkmn = (ItemVO)itemList.get(i);
                add(pkmn);

                notifyDataSetChanged();
            }

对此:

itemList = (ArrayList<ItemVO>)results.values;
notifyDataSetChanged(); 

试试这个:

private ArrayList<ItemVO> mItemList;

public Filter getFilter()
{
    return new Filter(){
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
                 final FilterResults oResults = new FilterResults();
                 final ArrayList<ItemVO> result = new ArrayList<ItemVO>();

                 If (mItemList == null)
                       mItemList = itemList;


                 if (constraint != null){
                      if (mItemList != null && mItemList.size > 0){
                          //loop
                          for (final ItemVO item : mItemList){
                          final String value = item.getItemdescription().toLowerCase();

                            if (value.startsWith(constraint.toString())){
                                    result.add(item);
                                 }
                          }
                       }
                  }
        }
       oResults.values = result;
    }

@SuppressWarnings("unchecked")
  @Override
  protected void publishResults(CharSequence constraint,FilterResults results) {
                itemList = (ArrayList<ItemVO>)results.values;
                           notifyDataSetChanged(); 

  } 
}

答案 1 :(得分:0)

我不确定我是否可以帮助你。试着改变这个:

CateringList.this.dataAdapter.getFilter().filter(cs);
dataAdapter.notifyDataSetChanged();

你以两种不同的方式访问dataAdapter,而且我认为你不需要在这里调用notifyDataSetChanged,所以改为:

dataAdapter.getFilter().filter(cs);

另外,如果要显示没有结果的列表rty:

dataAdapter.getFilter().filter("");

编辑:

添加此代码以侦听编辑文本更改:

private TextWatcher filterTextWatcher = new TextWatcher() {
    public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
    int count) {

        Log.d("Constants.TAG", "*** Search value changed: " + cs.toString());
        dataAdapter.getFilter().filter(cs);       
};

初始化EditText后:

searchBox.addTextChangedListener(filterTextWatcher);

编辑2:

改变这个:

if (prefix == null || prefix.length() == 0)
{
    //ArrayList<ItemVO> list = new ArrayList<ItemVO>(fiems);
    results.values = null;
    results.count = 0;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {

    itemList = (ArrayList<ItemVO>)results.values; // has the filtered values

    if(itemList != null) {
        clear();
        int count = itemList.size();
        for (int i=0; i<count; i++)
        {
            ItemVO pkmn = (ItemVO)itemList.get(i);
            add(pkmn);
        }

    notifyDataSetChanged(); // notifies the data with new filtered values
}

答案 2 :(得分:0)

我已经用这个解决方法解决了......希望这个也适合你:)

searchBox.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                        // Call back the Adapter with current character to Filter
                        Log.d("Constants.TAG", "*** Search value changed: " + cs.toString());
                        CateringList.this.dataAdapter.getFilter().filter(cs);

                       dataAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                       dataAdapter.itemList = OriginalItemList;
                    }

                    @Override
                    public void afterTextChanged(Editable arg0) {
                        //if(arg0.length()==0)
                       // {

                       // }
                    }
                });
在搜索搜索文本被更改之前

...只需将原始数据状态分配回适配器以再次过滤。

答案 3 :(得分:0)

这种情况正在发生,因为您的适配器的listItems已被删除,但是当您按退格键时,它们不会被添加回来。例如,当您按下A&#39; A&#39;时,您在recyclerview中共有10个项目。你得到4个过滤的项目。这里有6个项目从您的实际listItems中删除(之前有10个项目)。但这些被删除的物品从未加回。

<强>解决方案: 检查backpress并在过滤之前,为你创建fullList First的临时副本。并呼叫过滤。

答案 4 :(得分:0)

每次调用onQueryTextChange时,只需将数据刷新到您的recyclerview

    @Override
        public boolean onQueryTextChange(String newText) {
            
            rvAdapter = new RecyclerViewAdapter(Context, <list of files>);

            recyclerView.setAdapter(rvAdapter);
            rvAdapter.getFilter().filter(newText);
            return true;
        }
相关问题