如何添加过滤器方法在列表适配器中搜索记录在Android中?

时间:2013-07-09 07:23:59

标签: java android android-listview

这是我的代码。我想添加Filter方法来搜索自定义适配器中的list元素。我有很多示例代码但是很难在我的代码中集成。

 import java.util.ArrayList;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;

  class MyFSCustomAdapter extends BaseAdapter
    {

   String[] Name;
   Bitmap[] data_imageID;
   Context appcontext;

   String []dist;
   String []Address;

   MyFSCustomAdapter()
    {

    }


    MyFSCustomAdapter(String[] name, Bitmap[] imageID,String[] Dist,String[] Add,               Context cont)
    {
     Name = name;
     data_imageID = imageID;
      dist=Dist;
      Address=Add;
      appcontext=cont;
   }


    MyFSCustomAdapter(ArrayList<String> name, ArrayList<Bitmap> image,ArrayList<String>        Dist,ArrayList<String> Add)
    { 

      Name = new String[name.size()];
      data_imageID = new Bitmap[image.size()];
      dist= new String[Dist.size()];
      Address= new String[Add.size()];

     for(int i=0;i<name.size();i++)
     {
      Name[i] = name.get(i);
      data_imageID[i] = image.get(i);
      dist[i]=Dist.get(i);
      Address[i]=Add.get(i);
     }

    }
    public int getCount()
    {
     return Name.length;
    }

    public String getItem(int position)
    {
     return null;
    }

    public long getItemId(int position)
    {
     return position;
    }
   public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater =(LayoutInflater)        appcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=null;

   row = inflater.inflate(R.layout.listview_fs, parent, false);

   TextView txtName = (TextView) row.findViewById(R.id.Name);
   TextView txtMetrs = (TextView) row.findViewById(R.id.meters);
   TextView txtAddress = (TextView) row.findViewById(R.id.address);
   final ImageView imageview = (ImageView) row.findViewById(R.id.ImageView01);
   txtName.setText(Name[position]); 
   txtMetrs.setText(dist[position]);
   txtAddress.setText(Address[position]);
   imageview.setImageBitmap(data_imageID[position]);

           return (row);

       }



      }

这是我的清单。请帮我.... enter image description here

1 个答案:

答案 0 :(得分:0)

        lv = (ListView) findViewById(R.id.listView1);
      lv.setTextFilterEnabled(true);
     search=(EditText)findViewById(R.id.editText1);






        adapter = new MyCustomAdapter(this, R.layout.activity_main,arr);
         adapter.notifyDataSetChanged();
         lv.setAdapter(adapter);

        OnItemClickListener itemClickListener=new OnItemClickListener() {

             @Override


            public void onItemClick(AdapterView<?> parent, View view,int  position,
                    long id) {

                String label =  parent.getItemAtPosition(position).toString();

                 Toast.makeText(getBaseContext(),label,  Toast.LENGTH_SHORT).show();


             }
        };

        lv.setOnItemClickListener(itemClickListener);






     search.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int  arg2, int arg3) {


                  MainActivity.this.adapter.getFilter().filter(cs.toString());  
                MainActivity.this.adapter.notifyDataSetChanged(); 


               adapter.notifyDataSetChanged();


                lv.setAdapter(adapter);
            }

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


            }

            @Override
            public void afterTextChanged(Editable arg0) {



            }

        });