2014-11-13 37 views
3

嘿,大家好我試圖建成一個片段 列表視圖列表視圖來實現搜索功能工作正常,但是當我在EditText上鍵入要搜索消失實現搜索裏面片段

我的代碼:

public class DrinksFragment extends Fragment { 

    private View rootView; 
    private ArrayAdapter<DrinksList> adapter; 
    private List<DrinksList> drinks; 
    private ListView lv; 
    ArrayList<DrinksList> mAllData; 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false); 
     populateDrinksList(); 
     doSearch(); 
     return rootView; 
    } 

    private void doSearch() { 
     final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks); 
     et.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       String text = et.getText().toString().toLowerCase(Locale.getDefault()); 
      adapter.filter(text); 
      } 
     }); 
    } 



    private void populateDrinksList() { 
     drinks = new ArrayList<DrinksList>(); 
     drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50)); 
     drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50)); 
     drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50)); 
     drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50)); 
     drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50)); 
     drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50)); 
     drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50)); 
     drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50)); 
     drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50)); 
     drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50)); 
     drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50)); 
     drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00)); 
     drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00)); 
     drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50)); 
     lv = (ListView)rootView.findViewById(R.id.drinksListView); 
     adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks); 
     lv.setAdapter(adapter); 
    } 

}public class DrinksFragment extends Fragment { 

    private View rootView; 
    private ArrayAdapter<DrinksList> adapter; 
    private List<DrinksList> drinks; 
    private ListView lv; 
    ArrayList<DrinksList> mAllData; 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false); 
     populateDrinksList(); 
     doSearch(); 
     return rootView; 
    } 

    private void doSearch() { 
     final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks); 
     et.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       DrinksFragment.this.adapter.getFilter().filter(s); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
    } 



    private void populateDrinksList() { 
     drinks = new ArrayList<DrinksList>(); 
     drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50)); 
     drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50)); 
     drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50)); 
     drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50)); 
     drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50)); 
     drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50)); 
     drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50)); 
     drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50)); 
     drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50)); 
     drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50)); 
     drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50)); 
     drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00)); 
     drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00)); 
     drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50)); 
     lv = (ListView)rootView.findViewById(R.id.drinksListView); 
     adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks); 
     lv.setAdapter(adapter); 
    } 

} 

我的適配器:

public class MyCustomDrinksListAdapter extends ArrayAdapter<DrinksList> { 


    private List<DrinksList> items = null; 
    private ArrayList<DrinksList> arraylist; 
    public MyCustomDrinksListAdapter(Context context, int layoutId, List<DrinksList> items) { 
     super(context, layoutId, items); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View arrayView = convertView; 
     if(arrayView == null){ 
      LayoutInflater vi; 
      vi = LayoutInflater.from(getContext()); 
      arrayView = vi.inflate(R.layout.list_item, parent, false); 
     } 

     DrinksList currentPosition = getItem(position); 
     if(currentPosition != null){ 
      ImageView image = (ImageView)arrayView.findViewById(R.id.product_image_coffee); 
      image.setImageResource(currentPosition.getImage()); 

      TextView name = (TextView)arrayView.findViewById(R.id.product_name_coffee); 
      name.setText(currentPosition.getName()); 

      TextView price = (TextView)arrayView.findViewById(R.id.product_price_coffee); 
      price.setText(String.format("%.2f", currentPosition.getPrice())); 
     } 
     return arrayView; 
    } 

    public void filter(String charText) { 
     charText = charText.toLowerCase(Locale.getDefault()); 
     items.clear(); 
     if (charText.length() == 0) { 
      items.addAll(arraylist); 
     } else { 
      for (DrinksList wp : arraylist) { 
       if (wp.getName().toLowerCase(Locale.getDefault()) 
         .contains(charText)) { 
        items.add(wp); 
       } 
      } 
     } 
     notifyDataSetChanged(); 
    } 
} 

我嘗試以下的Android開發者的例子,但我不能。 在此先感謝! 有什麼想法?

+0

將代碼放在你的適配器中的getFilter()中 –

+0

我沒有一個名爲getFilter()的方法。它顯示了當我在適配器後添加點 –

+0

爲什麼不嘗試與baseadapter? – Dev

回答

7

而不是在Adapter類中添加過濾器方法。 ..添加它在你片段..這樣..

public class DrinksFragment extends Fragment { 

    private View rootView; 
    private ArrayAdapter<DrinksList> adapter; 
    private List<DrinksList> drinks; 
    private ListView lv; 
    ArrayList<DrinksList> mAllData=new ArrayList<DrinksList>(); 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false); 
     populateDrinksList(); 
     doSearch(); 
     return rootView; 
    } 

    private void doSearch() { 
     final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks); 
     et.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

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



    private void populateDrinksList() { 
     drinks = new ArrayList<DrinksList>(); 
     drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50)); 
     drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50)); 
     drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50)); 
     drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50)); 
     drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50)); 
     drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50)); 
     drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50)); 
     drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50)); 
     drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50)); 
     drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50)); 
     drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50)); 
     drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00)); 
     drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00)); 
     drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50)); 

mAllData.addAll(drinks); 
     lv = (ListView)rootView.findViewById(R.id.drinksListView); 
     adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(),  R.layout.list_item, drinks); 
     lv.setAdapter(adapter); 
    } 


public void filter(String charText) { 
     charText = charText.toLowerCase(Locale.getDefault()); 
     drinks .clear(); 
     if (charText.length() == 0) { 
      drinks.addAll(mAllData); 
     } else { 
      for (DrinksList wp : mAllData) { 
       if (wp.getName().toLowerCase(Locale.getDefault()) 
         .contains(charText)) { 
        drinks .add(wp); 
       } 
      } 
     } 
     notifyDataSetChanged(); 
    } 

} 

和你適配器類將保持原樣(刪除濾波器法)..

public class MyCustomDrinksListAdapter extends ArrayAdapter<DrinksList> { 


    // private List<DrinksList> items = null; // no use 
    private ArrayList<DrinksList> arraylist; 
    public MyCustomDrinksListAdapter(Context context, int layoutId, List<DrinksList> items) { 
     super(context, layoutId, items); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View arrayView = convertView; 
     if(arrayView == null){ 
      LayoutInflater vi; 
      vi = LayoutInflater.from(getContext()); 
      arrayView = vi.inflate(R.layout.list_item, parent, false); 
     } 

     DrinksList currentPosition = getItem(position); 
     if(currentPosition != null){ 
      ImageView image = (ImageView)arrayView.findViewById(R.id.product_image_coffee); 
      image.setImageResource(currentPosition.getImage()); 

      TextView name = (TextView)arrayView.findViewById(R.id.product_name_coffee); 
      name.setText(currentPosition.getName()); 

      TextView price = (TextView)arrayView.findViewById(R.id.product_price_coffee); 
      price.setText(String.format("%.2f", currentPosition.getPrice())); 
     } 
     return arrayView; 
    } 


} 

謝謝。希望它的工作...... !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! ............ 1

3

您的自定義適配器必須實現Filterable接口,並且您應該重寫getFilter()方法。

是指這樣的回答: How to implement getfilter() with custom adapter that extends baseadapter

+0

和getFilter方法我該怎麼辦? –

+0

用getFilter方法應該定義您要如何執行搜索時,由於您使用的自定義對象(DrinksList)不是原始數據類型的ArrayList。 –

+0

檢查我發佈的鏈接。 –

2

我想通了,問題是MyCustomDrinksListAdapter類的構造函數。在構造函數初始化 ArrayList的是這樣的:

arraylist = new ArrayList<DrinksList>(); 
this.arraylist.addAll(items); 

你並不需要從基礎適配器擴展只是改變你的構造函數。 看起來你並不熟悉ViewHolder的概念。在getView()中搜索並使用它可以提高性能。但你的剩餘代碼是正確的,但構造。 如果你需要任何其他幫助,你可以問。

+0

構造函數工作正常,因爲它和列表視圖運行正常...但是,爲什麼我應該使用vieholder類?什麼會改善?而且問題是關於在自定義適配器的列表視圖中實現搜索 –

+0

您需要更改構造函數的原因是有兩個列表,一個是項目,另一個是arraylist,它是永久性的。您需要初始化arrayList,否則您怎麼能跟蹤所有drinkItems?看看你的列表項目被用作臨時基礎,當你搜索例子「c」時,包含「c」的所有項目將被插入到數組列表中的項目中。如果你沒有在構造函數中初始化arraylist如何它可以成爲所有物品的永久存儲空間嗎?做到這一點,看看你的自我。 –

+0

而關於ViewHolder它會提高性能。查看findViewById()方法使用CPU爲什麼你需要找到一個你已經提到的項目?查看Holder的目的是檢查你是否已經有了像TextView這樣的控件的引用,然後再次找不到它。有一個示例http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example。 html –

1

這是你需要將它用來實現列表視圖一個srach代碼:從following教程採取

inputSearch.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
     // When user changed the Text 
     MainActivity.this.adapter.getFilter().filter(cs); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
      int arg3) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void afterTextChanged(Editable arg0) { 
     // TODO Auto-generated method stub       
    } 
}); 

。希望它有幫助:)