我希望根据名称过滤我的列表视图,但我的inputSearch.addTextChangedListener
无法正常工作
你可以帮我纠正吗
private void showProduct(String json){
JSONObject jsonObject = null;
ArrayList<Product> productList = new ArrayList<>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
String image1 = "http://aaaa.com/Scripts/images/"+jo.getString("image")+".jpg";
Product product = new Product();
product.setName(name);
product.setRef(ref);
product.setImageUrl(image1);
productList.add(product);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new CustomArrayAdapter(getContext(), productList);
listView.setAdapter(adapter);
}
inputsearch
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
((CustomArrayAdapter) ProduitsFragment.this.adapter).getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
CustomArrayAdapter类
public class CustomArrayAdapter extends ArrayAdapter<Product> {
private Context context;
private ArrayList<Product> productList;
public CustomArrayAdapter(Context context, ArrayList<Product> productList) {
super(context, R.layout.list_row2, productList);
this.context = context;
this.productList = productList;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_row2, parent, false);
TextView nom = (TextView) rowView.findViewById(R.id.nom);
TextView email2 = (TextView) rowView.findViewById(R.id.email2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
nom.setText(productList.get(position).getName()); //make sure nom is for name and email2 is for ref. This is just my guesswork.
email2.setText(productList.get(position).getRef());
Picasso.with(context).load(productList.get(position).getImageUrl()).into(imageView);
return rowView;
}
}
答案 0 :(得分:0)
public class CustomArrayAdapter extends ArrayAdapter<Product> implements Filterable {
private Context context;
private ArrayList<Product> productList;
ValueFilter valueFilter;
ArrayList<Product> mFilterList;
public CustomArrayAdapter(Context context, ArrayList<Product> productList) {
super(context, R.layout.list_row2, productList);
this.context = context;
this.productList = productList;
this.mFilterList = productList;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_row2, parent, false);
TextView nom = (TextView) rowView.findViewById(R.id.nom);
TextView email2 = (TextView) rowView.findViewById(R.id.email2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
nom.setText(productList.get(position).getName()); //make sure nom is for name and email2 is for ref. This is just my guesswork.
email2.setText(productList.get(position).getRef());
Picasso.with(context).load(productList.get(position).getImageUrl()).into(imageView);
return rowView;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.e("Here the value",constraint.toString());
final FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Product> firstFilterList = new ArrayList<Product>();
for (int i = 0; i < mFilterList.size(); i++) {
if ( (mFilterList.get(i).getName().toUpperCase().contains(constraint.toString().toUpperCase())||
mFilterList.get(i).getRef().toUpperCase().contains(constraint.toString().toUpperCase())
)) {
Product _product = new Product(
mFilterList.get(i).getRef() ,
mFilterList.get(i).getName()
);
firstFilterList.add(_product);
}
}
results.count = firstFilterList.size();
results.values = firstFilterList;
} else {
results.count = mFilterList.size();
results.values = mFilterList;
}
return results;
}
@Override
protected void publishResults(final CharSequence constraint,
final FilterResults results) {
productList = (ArrayList<Product>) results.values;
notifyDataSetChanged();
}
}
}
inputsearch 在Activity类中实现它
implements
SearchView.OnQueryTextListener
并覆盖以下两种方法
@Override
public boolean onQueryTextChange(String newText) {
newText = WordUtils.capitalize(newText);
adapter.getFilter().filter(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
并在你的创建中添加此
inputSearch.setOnQueryTextListener(Search_Books.this);