0

这是我的代码。我想添加过滤器方法来搜索自定义适配器中的列表元素。我有很多示例代码,但是很难集成到我的代码中。

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

       }



      }

这是我的清单。请帮我.... 在此处输入图像描述

4

1 回答 1

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



            }

        });
于 2013-07-09T09:26:57.420 回答