2015-11-06 77 views
0

我已經嘗試了幾個小時查看示例和其他人的代碼,但我仍然不太明白它。如何使用編輯文本過濾自定義ListView?

我有我自己的方法,其中一半工作。

public class List_of_recipes extends ActionBarActivity { 
    //original 
    public List<List_view_class> list_view_class; 
    ArrayAdapter<List_view_class> adapter; 

    // not the original 
    public List<List_view_class> another_list_class; 
    public ListView list; 
    public EditText edit_text; 
    //ActionBar actionBar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list_of_recipes); 

     // actionBar = getActionBar(); 
     //actionBar.setDisplayHomeAsUpEnabled(true); 
     //actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#A02727"))); 

     list = (ListView) findViewById(R.id.listView1); 
     edit_text = (EditText) findViewById(R.id.editText1); 
     list_view_class = new ArrayList<List_view_class>(); 
     another_list_class = new ArrayList<List_view_class>(); 
     adapter = new MyListAdapter(list_view_class); 

     list_view_class.add(new List_view_class(R.drawable.chicken, "Chicken salad", "15 min", "Easy", "4 serves")); 
     list_view_class.add(new List_view_class(R.drawable.mexican, "Mexican egg soup", "20 min", "Easy", "2 serves")); 
     list_view_class.add(new List_view_class(R.drawable.oyster, "Oysters with pancetta", "10 min", "Super Easy", "2 serves")); 
     //list_view_class.add(new List_view_class(R.drawable.wild_mushroom,"Wild mushroom crostini","5 min", "Easy","6 serves")); 

     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View viewclicked, int position, long id) { 
       // TODO Auto-generated method stub 
       if (position == 0) { 
        Intent i = new Intent(List_of_recipes.this, Chicken_salad.class); 
        startActivity(i); 
        finish(); 
       } 

       if (position == 1) { 
        Intent i = new Intent(List_of_recipes.this, Mexican_egg.class); 
        startActivity(i); 
        finish(); 
       } 

       if (position == 2) { 
        Intent i = new Intent(List_of_recipes.this, Oyster.class); 
        startActivity(i); 
        finish(); 
       } 
      } 
     }); 

     edit_text.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       // TODO Auto-generated method stub 
       String searchString = edit_text.getText().toString(); 
       another_list_class.clear(); 
       int textLength = edit_text.length(); 

       for (int i = 0; i < list_view_class.size(); i++) { 
        String name = list_view_class.get(i).gettitle().toString(); 

        if (textLength <= name.length()) { 
         if (searchString.equalsIgnoreCase(name.substring(0, textLength))) { 
          another_list_class.add(list_view_class.get(i)); 
         } 

        } 
       } 

       adapter.notifyDataSetChanged(); 
       adapter = new MyListAdapter(another_list_class); 
       list.setAdapter(adapter); 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // TODO Auto-generated method stub 
      } 
     }); 
    } 

    public class MyListAdapter extends ArrayAdapter<List_view_class> { 
     public MyListAdapter(List<List_view_class> object_class) { 
      super(List_of_recipes.this, R.layout.activity_list_class_view, object_class); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View rowView = convertView; 

      if (rowView == null) { 
       LayoutInflater inflater = (LayoutInflater) List_of_recipes.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       rowView = inflater.inflate(R.layout.activity_list_class_view, null, true); 
      } 

      List_view_class listclass = list_view_class.get(position); 
      ImageView image = (ImageView) rowView.findViewById(R.id.imageView1); 
      image.setImageResource(listclass.getrecipe_icon()); 
      TextView date = (TextView) rowView.findViewById(R.id.textView1); 
      date.setText(listclass.gettitle()); 
      TextView cook = (TextView) rowView.findViewById(R.id.cooktime); 
      cook.setText(listclass.getcook_time()); 
      TextView diff = (TextView) rowView.findViewById(R.id.difficultylvl); 
      diff.setText(listclass.getdifficulty()); 
      TextView serve = (TextView) rowView.findViewById(R.id.serving); 
      serve.setText(listclass.getmakes()); 

      return rowView; 
     } 
    } 
} 

它沒有給出任何錯誤,但是我得到的是第一個列表視圖項,不管我輸入什麼。

我不知道還有什麼要補充或我在做什麼錯在這裏。任何幫助將不勝感激,謝謝!

+0

你是什麼意思「過濾器」與edittexts?你想建立自己的Listview項目嗎? – Paul

+1

嘗試移動notifyDataSetChanged(),設置新的適配器後 – X3Btel

回答

1
there is no need to create another adapter again and again on edit text change.you can refresh list view by just calling notifyDataSetChanged on adapter. 

String searchString = s.toString(); 
    another_list_class.clear(); 
    for(int i =0; i< list_view_class.size(); i++){ 
      String name = list_view_class.get(i).gettitle().toString(); 
      if(name.contains(searchString)) 
      { 
        another_list_class.add(list_view_class.get(i)); 
      } 
    } 
    adapter.notifyDataSetChanged(); 


    equalsIgnoreCase returns true if and only if two strings are exactly equal(Ignoring case sensitive); 

You can also use startWith in place of contains 
One more thing set your adpater with another_list_class arraylist initially. 
because in this example we are changing search result in another_list_class . 

這是更好地把代碼afterTextChanged。

+0

incase notifyDatasetchanged()不工作,然後使用notifyDatasetInvalidate();因爲它會強制適配器更改 – Paul

+0

感謝您的時間納文,我理解,直到你說「set因爲在這個例子中,我們正在改變another_list_class的搜索結果「你能詳細說明這個部分嗎?不知道你的意思是什麼 –

+0

如果我們用arraylist1設置Adapter並且我們改變了arraylist2.than, arraylist1.thus中沒有任何更改通知適配器進行刷新,它將使用arraylist1填充listview,因爲它具有該引用,所以不會發生任何更改。 –

1

嘗試移動代碼來afterTextChanged

INT正文長度= edit_text.getText()=空edit_text.getText()的toString()長度():!?。0;

 for(int i =0; i< list_view_class.size(); i++) 
    { 
     String name = list_view_class.get(i).gettitle().toString(); 

     if(textLength <= name.length()){ 

      if(searchString.equalsIgnoreCase(name.substring(0,textLength))) 
      { 
       another_list_class.add(list_view_class.get(i)); 
      } 

     } 
    } 
+0

謝謝,我確實將代碼移動到了afterTextChanged,但仍然給了我相同的結果。 ( –

+0

)我已經更新了我的答案 –

相關問題