2016-08-12 86 views
0

晚上好, 確實,我找到了一些關於爲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。

編輯: 正如你在我的代碼中看到我已經實現了一個過濾器,但我的editText不工作!在編譯時沒有顯示錯誤!

預先感謝您。

+3

您的問題鏈接已損壞。 你還沒有添加任何問題的日誌消息,也沒有真正的問題,並且你的代碼不完整。 –

+0

這是鏈接:http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ –

+0

好的,但你的問題尚未澄清。你有什麼困難? –

回答

相關問題