Listview Baseadapter搜索功能

时间:2013-08-02 00:01:58

标签: android listview baseadapter textwatcher

我想在BaseAdapter中实现搜索功能。我制作了代码,但没有正确过滤。我已经四处寻找了一些例子,但没有一个能帮助我解决它。

这是我的MyAdapter:

public class ListViewAdapter extends BaseAdapter implements Filterable {

// Declare Variables
Context context;
LayoutInflater inflater;

List<HashMap<String, String>> data2;
List<HashMap<String, String>> filteredData;

ImageLoader imageLoader;

String TAG = "Søge Resultater: ";


public ListViewAdapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data2 = arraylist;
    imageLoader = new ImageLoader(context);

    //To start, set both data sources to the incoming data
    filteredData = arraylist;
}

public int getCount() 
{
    return filteredData.size();
}

    public Object getItem(int position) 
{
    return filteredData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView fraction_name;
    TextView fraction_typetext;
    TextView fraction_type;
    ImageView fraction_pictogram;

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

    View itemView = inflater.inflate(R.layout.list_catabc_item, parent, false);
    // Get the position from the results
    HashMap<String, String> resultp = new HashMap<String, String>();

    resultp = filteredData.get(position);

    // Locate the TextViews in listview_item.xml
    fraction_name = (TextView) itemView.findViewById(R.id.affaldabc_fraction_name);
    fraction_typetext = (TextView) itemView.findViewById(R.id.affaldabc_fraction_typetext); 
    fraction_type = (TextView) itemView.findViewById(R.id.affaldabc_fraction_type);
    // Locate the ImageView in listview_item.xml
    fraction_pictogram = (ImageView) itemView.findViewById(R.id.pictogram); 

    // Capture position and set results to the TextViews
    fraction_name.setText(resultp.get(CatAbc.TAG_fraction_name));
    fraction_typetext.setText(resultp.get(CatAbc.TAG_fraction_typetext));
    fraction_type.setText(" "+resultp.get(CatAbc.TAG_fraction_type));
    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class to download and cache
    // images
    imageLoader.DisplayImage(resultp.get(CatAbc.TAG_fraction_pictogram), fraction_pictogram);
    // Capture button clicks on ListView items
    itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Get the position from the results
            HashMap<String, String> resultp = new HashMap<String, String>();
            resultp = data2.get(position);

            AlertDialog alertDialog = new AlertDialog.Builder(context).create(); //Read Update
            alertDialog.setTitle(resultp.get(CatAbc.TAG_fraction_name));
            alertDialog.setMessage(resultp.get(CatAbc.TAG_fraction_typetext) +" " + resultp.get(CatAbc.TAG_fraction_type) +"\n" + resultp.get(CatAbc.TAG_fraction_description));

            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                      // here you can add functions
                   }
                });
                alertDialog.show();  //<-- See This!
            }
    });

    return itemView;
}

@Override
public Filter getFilter()
{
   return new Filter()
   {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence)
        {
            FilterResults results = new FilterResults();

            //If there's nothing to filter on, return the original data for your list
            if(charSequence == null || charSequence.length() == 0)
            {
                results.values = data2;
                results.count = data2.size();
            }
            else
            {
                ArrayList<HashMap<String,String>> filterResultsData = new ArrayList<HashMap<String,String>>();

                for(HashMap<String,String> data : data2)
                {
                    if(charSequence == data)
                    {
                        Log.d(TAG, " Dage = Testing 2 chargSquence: " + charSequence); 
                        Log.d(TAG, " Dage = Testing 3 data values string: " + data.values().toString()); 

                        filterResultsData.add(data);
                    }
                }            

                results.values = filterResultsData;
                results.count = filterResultsData.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults)
        {
            filteredData = (ArrayList<HashMap<String,String>>)filterResults.values;
            notifyDataSetChanged();
        }
    };
}
}

这是我的myActivity和textwatcher:

    protected void onPostExecute(String file_url) { 
        if(mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
        // Locate the listview in listview_main.xml
        adapter = new ListViewAdapter(CatAbc.this, abcList);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
    }
}

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) {
        CatAbc.this.adapter.getFilter().filter(s); 
           }
};

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我从未使用Filterable,但我已经积极地为我的BaseAdapter实现了一个过滤解决方案:

 private EditText editText;

 editText = (EditText) findViewById(R.id.yourId);

 editText.addTextChangedListener(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) {

            textLength = editText.getText().length();
            text_sort.clear();
            image_sort.clear();

            for (int i = 0; i < mTablesTitles.length; i++) {
                if (textLength <= mTablesTitles[i].length()) {
                    if (editText.getText().toString().equalsIgnoreCase((String)mTablesTitles[i].subSequence(0,textLength))) {
                        text_sort.add(mTablesTitles[i]);
                    }
                }
            }

            mDrawerList.setAdapter(new CustomAdapter(text_sort));

           }
          });