我想在我的 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);
}
};
谁能帮我解决这个问题?