簡體   English   中英

如何在Android Studio中包括對自定義列表視圖的搜索?

[英]How do I include a search for my custom listview in android studio?

這是來自Main Activity類的。 我有一個數組的arraylist,需要為listview創建一個搜索欄。

  adapter = new ArrayAdapter<String[]>(this, R.layout.list_view, android.R.id.text1, spellList)
    {
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = super.getView(position, convertView, parent);

            String[] entry = spellList.get(position);
            TextView text1 = (TextView) view.findViewById(android.R.id.text1);
            TextView text2 = (TextView) view.findViewById(android.R.id.text2);
            text1.setText(entry[0]);
            text2.setText(entry[1]);

            return view;
        }
    };
    wizList.setAdapter(adapter);

    searchText.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) {

        }
    });

這是字符串數組的Arraylist中的數據。 如果按咒語名稱搜索,則為首選。

final ArrayList<String[]> spellList;

public WizardDatabase()
{
    spellList = new ArrayList<>();

    spellList.add(new String[] { "Resistance", "Subject gains +1 on saving throws."});
    spellList.add(new String[] { "Acid Splash", "Orb deals 1d3 acid damage."});
    spellList.add(new String[] { "Detech Poison", "Detects poison in one creature or small object."});

public ArrayList<String[]> getList()
{
    return spellList;
}

謝謝。

如果僅是簡單的字符串搜索,則使用以下代碼

searchText.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) {
            if (s.length() > 3) { // it's for performance I've put 3 you can change it 
                adapter.getFilter().filter(s.toString());
            }
        }
    });

暫無
暫無

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

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