如何在一个Android活动中处理2个Listviews

时间:2014-02-12 05:11:03

标签: android android-intent android-listview

我在1个活动中创建了 2个列表视图

例如:蔬菜

 Fruits

 Meat

因此,当您点击Fruits时,我希望所有5种水果可以在第二个listView中显示。你能告诉我怎么做吗?我的代码如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listv);

    ListView menu= (ListView) findViewById(R.id.listView1);
    String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};



ListView menu2= (ListView) findViewById(R.id.listView2);
    String subitems[]= {"xxx","xxx","xxx","xxx","xxx","xxx"};



    menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
    menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems));

}

6 个答案:

答案 0 :(得分:1)

在第一个listview的SetOnItemClick适配器中为第二个listview设置适配器:

menu.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, check which item is it and then set adapter and display values in 2nd listview

            }
        });  

参见:
http://matrix-examplecode.blogspot.in/2011/11/listview-example.html
http://www.mkyong.com/android/android-listview-example/
http://developer.android.com/reference/android/widget/AdapterView.html

答案 1 :(得分:1)

## -----lets try this simple code. this will suites your need-----##

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listv);

ListView menu= (ListView) findViewById(R.id.listView1);
ListView menu2= (ListView) findViewById(R.id.listView2);
String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};
String subitems1[]= {"xxx1","xxx","xxx","xxx","xxx","xxx"};
String subitems2[]= {"xxx2","xxx","xxx","xxx","xxx","xxx"};
String subitems3[]= {"xxx3","xxx","xxx","xxx","xxx","xxx"};
String subitems4[]= {"xxx4","xxx","xxx","xxx","xxx","xxx"};
String subitems5[]= {"xxx5","xxx","xxx","xxx","xxx","xxx"};
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
// on item click listener
menu.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                // get the selected values from listview 1
                String val = items[arg2];

                if(val.equalsIgnoreCase("Fruits"))
                {
                // set the second listview 2 
                menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems1));
                }
                else if(val.equalsIgnoreCase("Vegetables"))
                {
                menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems2));
                }
                else if(val.equalsIgnoreCase("Jooses"))
                {
                menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems3));
                }
                else if(val.equalsIgnoreCase("Meat"))
                {
                menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems4));
                }
                else if(val.equalsIgnoreCase("Toys"))
                {
                menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems5));
                }

            }

    });

}

答案 2 :(得分:0)

在菜单(Listview)上注册onItemClickListener回调。

menu.setOnItemClickListener(new OnItemClickListener()
{
  @Override
  public void onItemClick (AdapterView<?> parent, View view, int position,
      long id)
  {
    /* TODO: Update menu2 adapter items */
    /* Position - determines the item that is being clicked on your menu listview,
       by that. you can render your second adapter based on the item selected */

    /* Call adapter notifyDatasetChanged method to inform adapter that its data set
       has changed. */
    menu2Adapter.notifyDatasetChanged();
  }
});

或其他设计可能。将第二个listview放在第二个活动上。每次你 选择一个项目。按下指向第二个列表视图的新活动..

答案 3 :(得分:0)

单击左侧列表视图中的项目时,将触发OnItemCLickListener。在回调中,更新第二个列表视图维护的基础数据并调用notifyDataSetChanged

答案 4 :(得分:0)

检查以下答案......我自己检查了这段代码。可能是你想做的事。

    ListView menu = (ListView) findViewById(R.id.listView1);
    String items[] = { "Fruits", "Vegetables", "Jooses", "Meat", "Toys", "Cookeys" };

    final ListView menu2 = (ListView) findViewById(R.id.listView2);
    final String subitems[] = { "xxx", "xxx", "xxx", "xxx", "xxx", "xxx" };

    menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
    menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            //Here YourActivity.this means the Context
            menu2.setAdapter(new ArrayAdapter<String>(YourActivity.this,
                    android.R.layout.simple_list_item_1, subitems));
        }
    });

答案 5 :(得分:0)

  

覆盖您的第一个listview(示例中的菜单)onItemClickListener   并根据您的需要准备子项目(在menu2中显示的列表)   或者你想要显示什么,并调用notifyDataSetChanged()   menu2 adapter(menu2.getAdapter()。notifyDataSetChanged();)。