2016-06-17 58 views
1

我想篩選就取決於我的名字列表視圖,但我的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; 
    } 
} 
+0

我們可能需要看到' CustomArrayAdapter'類。另外,如果您將適配器聲明爲該類型,則不需要該轉換。 –

+0

[自定義的getFilter在android自定義ArrayAdapter]可能的重複(http://stackoverflow.com/questions/19122848/custom-getfilter-in-custom-arrayadapter-in-android) –

+0

@ cricket_007我編輯我的文章 – Joelle

回答

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); 
+0

否,不要這樣做,它不僅不會起作用(getCount()將始終返回原來的'productList'大小),而且如果要將'Product'更改爲'Product2' /'',則從頭開始創建新適配器是毫無意義的。 ImportedProduct'/whatever,而是基於'ArrayAdapter'編寫一個通用適配器 – pskink