【问题标题】:Reusing the same ListView to display different data复用同一个ListView显示不同的数据
【发布时间】:2012-04-01 18:53:26
【问题描述】:

ListViews 一直是我的弱点,现在我正在练习将Listview 放在Listview 中。无论如何,我首先在我的程序开始时调用我的ListView,它会使用保存在我的strings.xml 中的数组加载它:

String[] departments = getResources().getStringArray(
                R.array.departments_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                departments));
        setContentView(R.layout.main);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

我想要做的是每次单击列表项时使用新的值数组更新此ListView。我尝试这样做的原因是因为我计划为每个位置设置 27 个具有不同值的不同数组,并且我觉得如果不是为每个项目数组创建 ListView,我的资源会更轻,我会更新这个ListView。我知道我可能不是这样做最有效的方式,但如果有另一种实现我的想法的方式,请告诉我。

   lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // When clicked, show a toast with the TextView text
                    switch (position) {
                    case 0:
                        try {


    //It is here that i dont know what to do, I was going to call 
//the Listview the same way i did previously using my setlistadapter, 
//but i kept getting errors about the code being undefined


                            String[] listitems1 = getResources().getStringArray(
                                    R.array.items_array);

                        } catch (ClassCastException e) {
                            Toast.makeText(getApplicationContext(), "Error",
                                    Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 1:
                        try {
                      //The listview will be changed again here
                        } catch (ClassCastException e) {
                            Toast.makeText(getApplicationContext(), "Error",
                                    Toast.LENGTH_SHORT).show();
                        }
                        break;
                    }
                };

            });

【问题讨论】:

    标签: android android-listview adapter listadapter


    【解决方案1】:

    您是否想过使用BaseAdapter 并将其设置为列表适配器 http://developer.android.com/reference/android/widget/BaseAdapter.html

    【讨论】:

    • 您可以扩展BaseAdapter,而不是使用ArrayAdapter,这将使您更好地控制数据,那就是如果我从一开始就清楚地理解了这个问题
    【解决方案2】:

    你的方法是错误的(如果我明白你在做什么)。而不是每次用户单击时都替换ListView 的适配器(并且只需设置一个新适配器即可)初始列表中的一个元素,您应该开始一个新的活动,通过单击的位置并在您的新活动中设置适配器一个ListView,基于该位置具有正确的数组。

    一个小例子:

    主类:

    /**
     * The main class with the initial 27 items array.
     */
    public class Class1 extends ListActivity {
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            // start the second activity that will show our array depending on the
            // position clicked
            Intent i = new Intent(this, Class2.class);
            // put the position in the Intent so we can know in the other activity
            // what array to load.
            i.putExtra("pos", position);
            startActivity(i);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // I just used a simple array of 2 items, you'll load your 27 items
            // array
            String[] items = { "1", "2" };
            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items));
        }
    
    }
    

    将根据先前选择的位置显示数组的辅助活动:

    public class Class2 extends ListActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // get the Intent that started the activity
            Intent i = getIntent();
            // find out what position did that other activity send to us.
            int position = i.getIntExtra("pos", -1);
            // load the ListView with an adapter based on the array that you
            // want(according to that position)
            if (position == 0) {
                // the first element in the main list
                String[] items = getResources().getStringArray(R.array.a1);
                setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, items));
            } else if (position == 1) {
                // the second element in the main list
                String[] items = getResources().getStringArray(R.array.a2);
                setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, items));
            } else {
                // etc
            }
        }
    
    }
    

    【讨论】:

    • 如果我为每个要放入列表视图的数组开始一个新活动,那么这会导致我有大约 27 个额外的类吗?那不好吗?我一直认为我应该尽量减少一切
    • @TheObliviator 我的答案是针对这样的情况,即您有一个包含 27 个项目的主列表,并且对于这 27 个项目中的每一个,您都有另一个值数组,您还想在列表中显示(这是我所理解的,不,你没有 27 个活动,你只有一个主要活动和一个活动来显示与点击位置相对应的数组,我可以给你一个例子)。如果这不是您想要的,您应该编辑您的问题,以便更好地解释您想要什么。
    • 这正是我想要的,当我放学回家后,我会尝试进一步研究它,但是是的,一个示例或某种伪代码会很有帮助
    • @TheObliviator 我添加了一个小例子。
    【解决方案3】:

    Luksprog 的回答确实是正确的,它对于许多级别的列表非常有用(您没有设置限制,只需在加载了正确列表的情况下不断产生新的活动实例)

    但是

    如果您的列表深度不超过 2 个级别,您可以使用 ExpandableListActivity 而不是 ListActivity,它基本上是您正在使用的单级列表的增强版本,它本机处理组折叠/展开,因此您不需要为每个子级别生成一个新活动。

    再次注意,这种方法仅适用于不超过 2 层的列表

    这里有一些来自 Google 的很好的例子:

    public class ExpandableList3 extends ExpandableListActivity {
        private static final String NAME = "NAME";
        private static final String IS_EVEN = "IS_EVEN";
    
        private ExpandableListAdapter mAdapter;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
            List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
            for (int i = 0; i < 20; i++) {
                Map<String, String> curGroupMap = new HashMap<String, String>();
                groupData.add(curGroupMap);
                curGroupMap.put(NAME, "Group " + i);
                curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
                //filling with dummy data...
                List<Map<String, String>> children = new ArrayList<Map<String, String>>();
                for (int j = 0; j < 15; j++) {
                    Map<String, String> curChildMap = new HashMap<String, String>();
                    children.add(curChildMap);
                    curChildMap.put(NAME, "Child " + j);
                    curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
                }
                childData.add(children);
            }
    
            // Set up our adapter
            mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
            setListAdapter(mAdapter);
        }
    
    }
    

    【讨论】:

    • 谢谢你们都给了我很好的答案,感谢有关 ExpandableListActivity 的信息,我将开始更频繁地使用它
    猜你喜欢
    • 2015-12-28
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2020-09-27
    相关资源
    最近更新 更多