簡體   English   中英

如何使用編輯文本過濾自定義ListView?

[英]How can I filter the custom ListView with edit Text?

我已經嘗試了幾個小時來瀏覽示例和其他人的代碼,但是我仍然不太了解。

我有自己的方法,一半有效。

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;
        }
    }
}

它沒有錯誤,但是無論輸入什么,我得到的只是第一個列表視圖項。

我不確定在這里還要添加​​什么或做錯了什么。 任何幫助將不勝感激,謝謝!

嘗試將代碼移至afterTextChanged

int textLength = edit_text.getText()!= null?edit_text.getText()。toString()。length():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));
             } 

         } 
     } 
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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM