[拍卖列表图像] [1]我正在尝试实现搜索功能,以通过特定的TextView(即ItemName TextView)在ListView中进行搜索,如“ Adidas Shoes”和“ Nike Shoes”之类的图像中所示。该TextView的ID为txtName。当前,使用这些代码,没有错误,但是搜索功能没有执行任何操作。如何通过查看txtName TextView实现搜索以实际搜索?
适配器:
public class AuctionListAdapter extends BaseAdapter implements Filterable {
ValueFilter valueFilter;
private Context context;
private int layout;
private ArrayList<Model> auctionList;
public AuctionListAdapter(Context context, int layout, ArrayList<Model> auctionList) {
this.context = context;
this.layout = layout;
this.auctionList = auctionList;
}
@Override
public int getCount() {
return auctionList.size();
}
@Override
public Object getItem(int position) {
return auctionList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Model> filterList = new ArrayList<Model>();
for (int i = 0; i < auctionList.size(); i++) {
if ((auctionList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Model model = new Model(auctionList.get(i).getId(),auctionList.get(i).getName(),
auctionList.get(i).getDescription(),auctionList.get(i).getPrice(),auctionList.get(i).getDuration()
,auctionList.get(i).getImage());
filterList.add(model);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = auctionList.size();
results.values = auctionList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
auctionList = (ArrayList<Model>) results.values;
notifyDataSetChanged();
}
}
private class ViewHolder{
ImageView imageView;
TextView txtName,txtDescription,txtPrice,txtDuration;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout,null);
holder.txtName=row.findViewById(R.id.txtName);
holder.txtDescription=row.findViewById(R.id.txtDescription);
holder.txtPrice=row.findViewById(R.id.txtPrice);
holder.txtDuration=row.findViewById(R.id.txtDuration);
holder.imageView=row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
Model model = auctionList.get(position);
holder.txtName.setText(model.getName());
holder.txtDescription.setText(model.getDescription());
holder.txtPrice.setText(model.getPrice());
holder.txtDuration.setText(model.getDuration());
byte[] auctionImage = model.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(auctionImage,0,auctionImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
AuctionList.java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu,menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
答案 0 :(得分:0)
在适配器内创建一个私有类
/**
* Custom filter for Brand list
* Filter content in brand list according to the search text
*/
private class BrandFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint!=null && constraint.length()>0) {
ArrayList<User> tempList = new ArrayList<User>();
// search content in Brand list
for (User user : brandList) {
if (mList.getBrandName().toLowerCase().contains(constraint.toString().toLowerCase())) {
tempList.add(user);
}
}
filterResults.count = tempList.size();
filterResults.values = tempList;
} else {
filterResults.count = friendList.size();
filterResults.values = friendList;
}
return filterResults;
}
/**
* Notify about filtered list to ui
* @param constraint text
* @param results filtered result
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (ArrayList<User>) results.values;
notifyDataSetChanged();
}
}
对您的适配器实施Filterable,然后将以下代码写入Override函数getfilter。
if (brandFilter == null) {
Filter = new BrandFilter();
}
return friendFilter;
只需在onQueryTextChange的活动中调用此方法
mAdapetr.getFilter().filter(newText);
答案 1 :(得分:0)
对于临时列表:
private ArrayList<Model> filteredList;
以及您的构造函数
this.filteredList = auctionList
发布结果时,/**
* Notify about filtered list to ui
* @param constraint text
* @param results filtered result
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (ArrayList<Model>) results.values;
notifyDataSetChanged();
}