将搜索过滤器添加到我的列表视图

时间:2016-08-12 14:30:51

标签: java android eclipse listview

晚上好, 确实,我找到了一些关于为ListView创建搜索栏的教程。我遵循的是:Android Adding Search Functionality to ListView。代码工作正常,但我不能适应我的列表。我不得不从头开始重启我的项目,因为我搞砸了。我是Android编程的初学者,真的需要帮助。这是我的代码: ShoppingListActivity.java

public class ShoppingListActivity extends Activity {

 private List<Produit> mCartList;
 private ProduitAdapter mProductAdapter;
 EditText inputSearch;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.shoppinglist);


  mCartList = ShoppingListHelper.getCartList();

  // Make sure to clear the selections
  for(int i=0; i<mCartList.size(); i++) {
   mCartList.get(i).selected = false;
  }


  // Create the list
  final ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
  mProductAdapter = new ProduitAdapter(mCartList, getLayoutInflater(), true);
  listViewCatalog.setAdapter(mProductAdapter);

  inputSearch = (EditText) findViewById(R.id.search);

  inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }

        @Override
        public void afterTextChanged(Editable arg0) { 
            String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
            mProductAdapter.filter(text);}
    });


  listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
     long id) {
    Intent productDetailsIntent = new Intent(getBaseContext(),ProduitDetailsActivity.class);
    productDetailsIntent.putExtra(ShoppingListHelper.PRODUCT_INDEX, position);
    startActivity(productDetailsIntent);
   }
  });

 }

这包含ShoppingListHelper.java:

public static List<Produit> getCatalog(Resources res){
     if(catalog == null) {
      catalog = new Vector<Produit>();
      catalog.add(new Produit("Nutella", res
        .getDrawable(R.drawable.nutella),
        "Pate à tartiner au chocolat, au bon goût de noisettes", 750));
     }

     return catalog;
    }

和ProduitAdapter.java

     public class ProduitAdapter extends BaseAdapter {


 private List<Produit> mProductList;
 private LayoutInflater mInflater;
 private boolean mShowQuantity;


 private ArrayList<Produit> arraylist;
 public ProduitAdapter(List<Produit> list, LayoutInflater inflater, boolean showQuantity) {
  mProductList = list;
  mInflater = inflater;
  mShowQuantity = showQuantity;
 }

 @Override
 public int getCount() {
  return mProductList.size();
 }

 @Override
 public Object getItem(int position) {
  return mProductList.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }


 public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        mProductList.clear();
        if (charText.length() == 0) {
            mProductList.addAll(arraylist);
        } 
        else 
        {
            for (Produit wp : arraylist) 
            {
                if (wp.getProduct().toLowerCase(Locale.getDefault()).startsWith(charText)) 
                {
                    mProductList.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    } 

我使用eclipse JUNO。

EDITED: 正如您在我的代码中看到的,我已经实现了一个过滤器,但我的editText不起作用!在编译中没有显示错误!

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

查看本教程,将searchview添加到操作栏:http://javapapers.com/android/android-searchview-action-bar-tutorial/