我想在android中的ListView上应用搜索过滤功能。
我已经创建了CustomBaseAdapter适配器。当我在输入搜索中输入内容时,我在我的logcat中的publishResults arrayList
中获得了数组列表。 butListView没有更新。
CustomBaseAdapter
代码,
public class CustomBaseAdapter extends BaseAdapter implements Filterable{
Context context;
List<RowItem> rowItems;
List<String> arrayList;
List<String> mOriginalValues; // Original Values
LayoutInflater inflater;
public CustomBaseAdapter( Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
//int position = 2;
arrayList = new ArrayList<String>();
mOriginalValues=new ArrayList<String>();
for(int i = 0; i < rowItems.size(); i++)
{
RowItem rowItem = (RowItem)getItem(i);
mOriginalValues.add(rowItem.getTitle());
}
Log.d(mOriginalValues.toString(),"mOriginalValues");
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.label);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public RowItem getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
Log.d(results.values.toString(),"publishResults");
arrayList = (List<String>) results.values; // has the filtered values
Log.d(results.values.toString(),"publishResults arrayList");
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<String> arrayList = new ArrayList<String>();// Holds the results of a filtering operation in values
List<String> FilteredArrList = new ArrayList<String>();
if (mOriginalValues == null) {
// mOriginalValues = new ArrayList<String>(arrayList); // saves the original data in mOriginalValues
mOriginalValues.addAll(arrayList);
}
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
Log.d(constraint.toString().toLowerCase(),"constraint");
Log.d(mOriginalValues.toString(),"mOriginalValues");
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i);
if (data.toLowerCase().contains(constraint.toString())) {
Log.d(data,"Filtered data");
FilteredArrList.add(data);
}
Log.d(FilteredArrList.toString(),"FilteredArrList");
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
我的onCreate
中的 Activity
方法如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i],titles[i], TypeName[i] );
rowItems.add(item);
}
listView = (ListView)findViewById(R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new CustomBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
// Assign adapter to ListViez
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
答案 0 :(得分:1)
在适配器的构造函数()中进行以下更改,
//int position = 2;
arrayList = new ArrayList<String>();
mOriginalValues=new ArrayList<String>();
for(int i = 0; i < rowItems.size(); i++)
{
RowItem rowItem = (RowItem)getItem(i);
mOriginalValues.add(rowItem.getTitle());
arrayList.add(rowItem.getTitle());
}
Log.d(mOriginalValues.toString(),"mOriginalValues");
在getView()中进行以下更改,
if(arrayList == null || arrayList.size() == 0){
arrayList = mOriginalValues;
}
for(String Title : arrayList)
if(rowItem.getTitle().equals(Title)){
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
}
}
如果还有任何问题,请检查一下,
public class CustomBaseAdapter extends BaseAdapter implements Filterable {
Context context;
List<RowItem> rowItems;
List<L> mOriginalValues; // Original Values
LayoutInflater inflater;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
//int position = 2;
mOriginalValues = new ArrayList<>();
if(rowItems!=null) {
this.mOriginalValues.addAll(rowItems);
}
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.label);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(rowItems.get(position).getTitle());
holder.imageView.setImageResource(rowItems.get(position).getImageId());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public RowItem getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
Log.d(results.values.toString(),"publishResults");
rowItems = (List<RowItem>) results.values; // has the filtered values
Log.d(results.values.toString(),"publishResults arrayList");
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<RowItem> FilteredArrList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
Log.d(constraint.toString().toLowerCase(),"constraint");
Log.d(mOriginalValues.toString(),"mOriginalValues");
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i).getTitle();
if (data.toLowerCase().contains(constraint.toString())) {
Log.d(data,"Filtered data");
FilteredArrList.add(mOriginalValues.get(i));
}
Log.d(FilteredArrList.toString(),"FilteredArrList");
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}