重用相同的ListView来显示不同的数据

时间:2012-04-01 18:53:27

标签: android android-listview adapter listadapter

ListViews一直是我的弱点,现在我正在练习ListviewListview。无论如何,我首先在我的程序开始时调用我的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;
                    }
                };

            });

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

你的做法是错的(如果我明白你在做什么)。每次用户点击(并且只是设置一个新的适配器应该工作)时,不应在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
        }
    }

}

答案 2 :(得分:1)

Luksprog的答案确实是正确的,它对于很多级别的列表非常有用(你没有限制,只是在加载了正确的列表的情况下继续产生新的活动实例)

BUT

如果您的列表深度不超过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);
    }

}