使用 EditText 在 ListView 中搜索

Posted

技术标签:

【中文标题】使用 EditText 在 ListView 中搜索【英文标题】:Search in ListView with EditText 【发布时间】:2012-06-04 16:27:27 【问题描述】:

我有一个ListView。它的每一行包含 3 个TextView 和 2 个Button。在它上面我有一个EditText。如何通过每行的第一个 TextView 中的值过滤我的 ListView

【问题讨论】:

samir-mangroliya.blogspot.in/2012/05/… 检查这个:android -search listview 将“serach”更改为“search”以获得更好的参考 【参考方案1】:

为列表视图创建一些数组列表。在“AddTextChangeListener”上,您可以搜索列表中的类似项目并为搜索的文本加载新的数组列表...

edittext.addTextChangedListener(new TextWatcher()
public void afterTextChanged(Editable s) 
     //search for the keyword and add the items to a new arraylist

【讨论】:

【参考方案2】:

以下代码使用自定义列表适配器。

我使用自定义列表适配器显示了 3 个文本视图。我制作了一个示例,它根据列表顶部的 editext 中的输入进行搜索。根据输入的数据与每一行的textview1中的数据进行比较,并相应地过滤和显示数据。

列表视图行中可以有其他项目,搜索可以基于 texview1/textview2 或 textview3。根据您的需要修改以下内容。

public class MainActivity extends Activity 
ArrayList<NewData> mTemp=new ArrayList<NewData>();
ArrayList<NewData> mPostingData=new ArrayList<NewData>();
ArrayList< NewData> mOri = new ArrayList<NewData>();

Myadapter ma;
EditText search;

NewData nd;

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    for(int i = 0; i < 20; i++)
    

        nd=new NewData();

        nd.newDatacus.put(NewData.TAG_CUSTOMER_CODE, "i"+i);
        nd.newDatacus.put(NewData.TAG_CUSTOMER_NAME, "a"+i);
        nd.newDatacus.put(NewData.TAG_CUSTOMER_MOBILE, "number");
        nd.newDatacus.put(NewData.TAG_CUSTOMER_ADDRESS, "address");
        mOri.add(nd);
    

    ma= new Myadapter(MainActivity.this);
    mPostingData=mOri;
    mTemp=mOri;
    ListView lv= (ListView) findViewById(R.id.list);
    lv.setAdapter(ma);
    search= (EditText) findViewById(R.id.search);
    search.addTextChangedListener(new TextWatcher() 

        public void onTextChanged(CharSequence s, int start, int before, int count) 
            ma.getFilter().filter(s);
            ma.notifyDataSetChanged();

        

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


        

        public void afterTextChanged(Editable s) 
        
    );






class Myadapter extends ArrayAdapter

    LayoutInflater mInflater;


    public void setData(ArrayList<NewData> mPpst)    
        mPostingData = mPpst;//contains class items data.
    

    @Override
    public Filter getFilter() 
        return new Filter() 
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) 
                if (results != null && results.count >= 0) 
                    setData((ArrayList<NewData>) results.values);//if results of search is null set the searched results data
                 else 
                    setData(mOri);// set original values
                

                notifyDataSetInvalidated();
            



            @Override
            protected FilterResults performFiltering(CharSequence constraint) 
                FilterResults result = new FilterResults();
                if (!TextUtils.isEmpty(constraint)) 
                    constraint = constraint.toString().toLowerCase();
                    ArrayList<NewData> foundItems = new ArrayList<NewData>();
                    if(mTemp!=null)
                    
                        for(int i=0;i<mTemp.size();i++)
                        

                            if (mTemp.get(i).newDatacus.get(NewData.TAG_CUSTOMER_CODE).toString().contains(constraint)) 
                                System.out.println("My datas"+mTemp.get(i).newDatacus.get(NewData.TAG_CUSTOMER_CODE).toString());
                                foundItems.add(mTemp.get(i));

                            
                            else
                            

                            
                        
                    
                    result.count = foundItems.size();//search results found return count
                    result.values = foundItems;// return values
                 
                else
                
                    result.count=-1;// no search results found
                


                return result;
            
        ;
    
    public Myadapter(Context context) 
        super(context, 0);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // TODO Auto-generated constructor stub
    

    @Override
    public int getCount() 
        // TODO Auto-generated method stub
        return mPostingData.size();
    

    @Override
    public Object getItem(int position) 
        // TODO Auto-generated method stub
        return position;
    

    @Override
    public long getItemId(int position) 
        // TODO Auto-generated method stub
        return 0;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        // TODO Auto-generated method stub
        ViewHolder holder;


        if(mOri == null )

            return null;
        
        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if (convertView == null) 
            convertView = mInflater.inflate(R.layout.list, null);
            convertView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.t1=(TextView) convertView.findViewById(R.id.textView1);
            holder.t2 = (TextView) convertView.findViewById(R.id.textView2);
            holder.t3 = (TextView) convertView.findViewById(R.id.textView3);

            convertView.setTag(holder);
         else 
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        
        holder.t1.setText(mPostingData.get(position).newDatacus.get(NewData.TAG_CUSTOMER_CODE).toString());
        holder.t2.setText(mPostingData.get(position).newDatacus.get(NewData.TAG_CUSTOMER_NAME).toString());
        holder.t3.setText(mPostingData.get(position).newDatacus.get(NewData.TAG_CUSTOMER_MOBILE).toString());
        return convertView;
    

class ViewHolder

    TextView t1,t2,t3;

Main.xml

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/search"
    android:layout_
    android:layout_ />

<ListView
    android:id="@+id/list"
    android:layout_
    android:layout_ />

List.xml //要膨胀的xml

<TextView
    android:id="@+id/textView1"
    android:layout_
    android:layout_
    android:paddingLeft="20dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_
    android:layout_
    android:paddingLeft="20dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_
    android:layout_
    android:paddingLeft="20dp"
    android:text="TextView" />

新数据类

 public class NewData 
  public static final String TAG_CUSTOMER_CODE = "customer_code";
  public static final String TAG_CUSTOMER_NAME = "customer_name";
  public static final String TAG_CUSTOMER_MOBILE = "customer_mobile";
  public static final String TAG_CUSTOMER_ADDRESS = "customer_address";

  Hashtable newDatacus=new Hashtable();

  public NewData()
  

    newDatacus.put(NewData.TAG_CUSTOMER_CODE,new String());
    newDatacus.put(NewData.TAG_CUSTOMER_ADDRESS,new String());
    newDatacus.put(NewData.TAG_CUSTOMER_NAME,new String());
    newDatacus.put(NewData.TAG_CUSTOMER_MOBILE,new String());
    newDatacus.put(NewData.TAG_CUSTOMER_ADDRESS,new String());




 

【讨论】:

【参考方案3】:

点击链接

http://marakana.com/forums/android/learning_android_book/617.html

http://androidsearchfilterlistview.blogspot.in/2011/06/android-custom-list-view-filter.html

How can I filter ListView data when typing on EditText in android

How to dynamically update a ListView on Android

【讨论】:

以上是关于使用 EditText 在 ListView 中搜索的主要内容,如果未能解决你的问题,请参考以下文章

使用 EditText 过滤 SimpleCursorAdapter 支持的 ListView

在android中使用edittext对ListView进行排序

ListView + EditText => 糟糕的键盘行为

带有edittext的Listview - 在“下一个”上自动滚动

如何获取listview的item中的内容

Android使用EditText+Listview实现搜索效果(使用room模糊查询)