簡體   English   中英

Android 搜索擴展列表視圖

[英]Android Search Expandable ListView

我在構建我的第一個 android 應用程序時遇到了問題。 在應用程序中,我有一個使用 ListView 的搜索 function。 用戶可以在這里搜索某個項目並查看它是否存在。 這非常有效,我是根據本教程完成的: Android ListView with Search

我的所有項目都在 array.xml 中定義的字符串數組中找到。 現在,每個項目都可以是一個或多個 collections 的一部分。目前我顯示 collections 它包含在項目名稱旁邊,如下所示:“ ITEM -> Collection(s) ”。 這可行,但並不是真正優雅的選擇。 我想要做的是將項目的名稱作為可擴展 ListView 中的 ITEM,並將它所屬的 collections 作為 SUBITEM。 因此,我會有 2 個字符串 arrays,一個用於父項,一個用於子項。

我發現的所有示例都是“手動”填充列表,我無法弄清楚如何將我的 ITEMS 數組分配給 GROUP 並將我的 COLLECTIONS 數組分配給 CHILDREN。

我不知道我是否解釋得足夠清楚,但簡而言之,它會是這樣的:

ITEM_1 = 第一組名稱 COLLECTIONS_1 = 第一組的孩子

示例圖片

謝謝!

編輯:好的,設法像我想要的那樣創建可擴展視圖:

public class Search extends ListActivity {
    private String[] l1;
    private String[] l2;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        ArrayList<Map<String, String>> list = buildData();
        String[] from = { "name", "purpose" };
        int[] to = { android.R.id.text1, android.R.id.text2 };
        SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to);
        setListAdapter(adapter);
    }

    private ArrayList<Map<String, String>> buildData() {
        l1 = getResources().getStringArray(R.array.items);
        l2 = getResources().getStringArray(R.array.collections);
        Integer i;
        int to = l1.length;
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();     
        for(i=0;i < to;i++){
        list.add(putData(l1[i], l2[i]));
        }
        return list;
    }

    private HashMap<String, String> putData(String name, String purpose) {
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("name", name);
        item.put("purpose", purpose);
        return item;
    }
}

現在的問題是,我該如何排序?

你從這里下載源代碼( Expandablelistview with searchview in android

主活動.java:

public class MainActivity extends AppCompatActivity {
    ExpandableListView ev_list;
    List> lv_country = new ArrayList<>();
    CustomAdapter customAdapter;
    EditText et_search;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        init();

    }

    private void init() {
        ev_list = (ExpandableListView) findViewById(R.id.ev_list);
        et_search = (EditText) findViewById(R.id.et_search);

        fn_arraylist();
        customAdapter = new CustomAdapter(lv_country, getApplicationContext());
        ev_list.setAdapter(customAdapter);

        ev_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true; // This way the expander cannot be collapsed
            }
        });

        for (int i = 0; i < customAdapter.getGroupCount(); i++) {
            ev_list.expandGroup(i);
        }


        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if (et_search.getText().toString().length() == 0) {
                    customAdapter = new CustomAdapter(lv_country, getApplicationContext());
                    ev_list.setAdapter(customAdapter);

                    for (int i = 0; i < customAdapter.getGroupCount(); i++) {
                        ev_list.expandGroup(i);
                    }
                } else {

                    List> lv_search = new ArrayList<>();

                    for (int i = 0; i < lv_country.size(); i++) {
                        List> lv_searchstate = new ArrayList<>();

                        List> lv_state = (List>) lv_country.get(i).get("State");
                        for (int j = 0; j < lv_state.size(); j++) {
                            if (lv_state.get(j).get("Name").toString().toLowerCase().contains(et_search.getText().toString())) {
                                lv_searchstate.add(lv_state.get(j));
                            }
                        }

                        if (lv_searchstate.size() != 0) {
                            HashMap hashMap_search = new HashMap<>();
                            hashMap_search.put("Name", lv_country.get(i).get("Name").toString());
                            hashMap_search.put("State", lv_searchstate);

                            lv_search.add(hashMap_search);
                        }
                    }


                    customAdapter = new CustomAdapter(lv_search, getApplicationContext());
                    ev_list.setAdapter(customAdapter);

                    for (int i = 0; i < customAdapter.getGroupCount(); i++) {
                        ev_list.expandGroup(i);
                    }


                }

            }
        });


    }

謝謝!

暫無
暫無

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

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