我想篩選就取決於我的名字列表視圖,但我的inputSearch.addTextChangedListener
不起作用 你可以幫我糾正如何過濾android中的listview?
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;
}
}
我們可能需要看到' CustomArrayAdapter'類。另外,如果您將適配器聲明爲該類型,則不需要該轉換。 –
[自定義的getFilter在android自定義ArrayAdapter]可能的重複(http://stackoverflow.com/questions/19122848/custom-getfilter-in-custom-arrayadapter-in-android) –
@ cricket_007我編輯我的文章 – Joelle