0

我正在构建我的第一个 android 应用程序,但遇到了一个问题。在应用程序中,我有一个使用 ListView 的搜索功能。在这里,用户可以搜索某个项目并查看它是否存在。这真的很好,我是根据这个教程做的:Android ListView with Search

我的所有项目都在 array.xml 中定义的字符串数组中找到。现在,每个项目都可以是一个或多个集合的一部分。目前我在项目名称旁边显示它包含的集合,如下所示:“ ITEM -> Collection(s) ”。这可行,但并不是一个优雅的选择。我想做的是将项目的名称作为可扩展 ListView 中的 ITEM,并将它所属的集合作为 SUBITEM。因此,我将有 2 个字符串数组,一个用于父项,一个用于子项。

我发现的所有示例都是“手动”填充列表,我无法弄清楚如何将我的项目数组分配给 GROUP 并将我的 COLLECTIONS 数组分配给 CHILDREN。

我不知道我是否解释得足够清楚,但简而言之,它会是这样的:

ITEM_1 = 第一组名称 COLLECTIONS_1 = 第一组的孩子

示例图像

谢谢!

4

2 回答 2

0

编辑:好的,设法像我想要的那样创建可扩展视图:

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

现在的问题是,我该如何排序?

于 2013-04-03T16:05:07.267 回答
0

您从这里下载源代码(Expandablelistview with searchview in android

MainActivity.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);
                    }


                }

            }
        });


    }

谢谢!

于 2018-02-06T10:20:20.253 回答