Good evening, It's true that I have found some tutorial on creating a search bar for ListView. Among those that I followed is: Android Adding Search Functionality to ListView . The code works fine but I can not adapt it to my list. I had to restart my project from the beginning because I messed. I am a beginner in Android programming and really need help. Here is my code: 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);
}
});
}
and this is what contains 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;
}
And 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();
}
I use eclipse JUNO.
EDITED: As you see in my code I've implemented a filter but my editText don't work! in compilation no errors shown!
Thank you in advance.
查看此教程,将searchview添加到操作栏: http ://javapapers.com/android/android-searchview-action-bar-tutorial/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.