【问题标题】:using multiple listviews with simple adapter and hashmap使用带有简单适配器和哈希图的多个列表视图
【发布时间】:2014-06-04 13:45:48
【问题描述】:

大家好,希望你能帮到你,感谢你抽出宝贵的时间来看看

我试图通过一个简单的适配器和一个 hashmap 在一个活动中使用 multilpe 列表视图

这是我的一些代码......

final ArrayList<HashMap<String,String>> listGeneral = new ArrayList<HashMap<String,String>>();

在onCreate之前然后在onCreate中

setContentView(R.layout.custom_list_view);

        SimpleAdapter adapter1 = new SimpleAdapter(
                this,
                listGeneral,
                R.layout.custom_row_view,
                new String[] {"catGeneral","score1"},
                new int[] {R.id.text1,R.id.text2}
        );

setList1();
 setListAdapter(adapter1);

setList1

public void setList1(){

        HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("catGeneral","General Knowledge Lv1");
        temp.put("score1", level3);
        listGeneral.add(temp);

        HashMap<String,String> temp1 = new HashMap<String,String>();
        temp1.put("catGeneral","General Knowledge Lv2");
        temp1.put("score1", level4);
        listGeneral.add(temp1);

        HashMap<String,String> temp2 = new HashMap<String,String>();
        temp2.put("catGeneral","General Knowledge Lv3");
        temp2.put("score1", level2);
        listGeneral.add(temp2);
}

和项目监听器

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);


        if (position == 0){
        Toast.makeText(this, "one", Toast.LENGTH_LONG).show();
        }
        else if (position == 1){
            Toast.makeText(this, "two", Toast.LENGTH_LONG).show();
            }
        else if (position == 2){
            Toast.makeText(this, "three", Toast.LENGTH_LONG).show();
            }
        else if (position == 3){
            Toast.makeText(this, "four", Toast.LENGTH_LONG).show();
            }
        else if (position == 4){
            Toast.makeText(this, "five", Toast.LENGTH_LONG).show();
            }
       }

和xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000fff"
        android:layout_weight="2"
        android:drawSelectorOnTop="false">
    </ListView>

    <TextView android:id="@id/android:empty"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="#FFff00"
        android:text="No data"
    />

</LinearLayout>

所有这些都适用于一个列表视图。但是当我添加第二个列表时

final ArrayList<HashMap<String,String>> listPopCulture = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter2 = new SimpleAdapter(
                this,
                listPopCulture,
                R.layout.custom_row_view,
                new String[] {"catPopCulture","score2"},
                new int[] {R.id.text1,R.id.text2}
        );


setList2();
setListAdapter(adapter2);

public void setList2(){

        HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("catPopCulture","The 60's");
        temp.put("score2", level3);
        listPopCulture.add(temp);

        HashMap<String,String> temp1 = new HashMap<String,String>();
        temp1.put("catPopCulture","The 70's");
        temp1.put("score2", level4);
        listPopCulture.add(temp1);

        HashMap<String,String> temp2 = new HashMap<String,String>();
        temp2.put("catPopCulture","The 80's");
        temp2.put("score2", level2);
        listPopCulture.add(temp2);
}

我不确定如何为第二个列表执行第二个 itemListener,以及如何调用第二个列表,因为我认为第一个列表是由 android:id="@id/android:list" 调用的 我尝试过的一切都没有奏效......

是不是因为简单的适配器因为它的 id 而不能在每个活动上设置一个以上的列表?以及如何在一个 xml 文件中声明第二个或多个列表视图。最后我想要一个带有滚动视图的布局,它包含一个文本视图然后是列表视图,然后是另一个文本视图和另一个列表视图......任何帮助都会很棒

这里几乎是他们的编辑答案。有效,但我需要 list2 和 list3 的 onItemListener

setContentView(R.layout.custom_list_view);

        ListView list2 = (ListView)findViewById(R.id.list2);
        ListView list3 = (ListView)findViewById(R.id.list3);



        SimpleAdapter adapter1 = new SimpleAdapter(
                this,
                listGeneral,
                R.layout.custom_row_view,
                new String[] {"catGeneral","score1"},
                new int[] {R.id.text1,R.id.text2}
        );
        //list1.setAdapter(adapter1);


        SimpleAdapter adapter2 = new SimpleAdapter(
                this,
                listPopCulture,
                R.layout.custom_row_view,
                new String[] {"catPopCulture","score2"},
                new int[] {R.id.text1,R.id.text2}
        );

        SimpleAdapter adapter3 = new SimpleAdapter(
                this,
                listKids,
                R.layout.custom_row_view,
                new String[] {"catKids","score3"},
                new int[] {R.id.text1,R.id.text2}
        );

        setList1();
        setList2();
        setList3();

                setListAdapter(adapter1);
        list2.setAdapter(adapter2);
        list3.setAdapter(adapter3);

        //end oncreate  
    }

    public void setList1(){

        HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("catGeneral","General Knowledge Lv1");
        temp.put("score1", level3);
        listGeneral.add(temp);

        HashMap<String,String> temp1 = new HashMap<String,String>();
        temp1.put("catGeneral","General Knowledge Lv2");
        temp1.put("score1", level4);
        listGeneral.add(temp1);

        HashMap<String,String> temp2 = new HashMap<String,String>();
        temp2.put("catGeneral","General Knowledge Lv3");
        temp2.put("score1", level2);
        listGeneral.add(temp2);

        HashMap<String,String> temp3 = new HashMap<String,String>();
        temp3.put("catGeneral","General Knowledge Lv4");
        temp3.put("score1", level3);
        listGeneral.add(temp3);

        HashMap<String,String> temp4 = new HashMap<String,String>();
        temp4.put("catGeneral", "General Knowledge Lv5");
        temp4.put("score1", level1);
        listGeneral.add(temp4);

        HashMap<String,String> temp5 = new HashMap<String,String>();
        temp5.put("catGeneral", "General Knowledge Lv6");
        temp5.put("score1", level1);
        listGeneral.add(temp5);
    }

列表 1 的工作监听器

 //list 1 listener
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            if (position == 0){
            Toast.makeText(this, "one", Toast.LENGTH_LONG).show();
            }
            else if (position == 1){
                Toast.makeText(this, "two", Toast.LENGTH_LONG).show();
                }
            else if (position == 2){
                Toast.makeText(this, "three", Toast.LENGTH_LONG).show();
                }
            else if (position == 3){
                Toast.makeText(this, "four", Toast.LENGTH_LONG).show();
                }
            else if (position == 4){
                Toast.makeText(this, "five", Toast.LENGTH_LONG).show();
                }
           }

我在 list2 onItemClick 上试过这个

list2.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                if (position == 0){
                    Toast.makeText(this, "one", Toast.LENGTH_LONG).show();
                    }
                    else if (position == 1){
                        Toast.makeText(this, "two", Toast.LENGTH_LONG).show();
                        }
                    else if (position == 2){
                        Toast.makeText(this, "three", Toast.LENGTH_LONG).show();
                        }
                    else if (position == 3){
                        Toast.makeText(this, "four", Toast.LENGTH_LONG).show();
                        }
                    else if (position == 4){
                        Toast.makeText(this, "five", Toast.LENGTH_LONG).show();
                        }
            }
        }); 

关于list2 和list3 onItemClick 的任何想法?

【问题讨论】:

    标签: android listview hashmap simpleadapter


    【解决方案1】:

    在你的 xml 中放两个列表:-

    <?xml version="1.0" encoding="utf-8"?>
    

    <ListView android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000fff"
        android:layout_weight="2"
        android:drawSelectorOnTop="false">
    </ListView>
    
    <ListView android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000fff"
        android:layout_weight="2"
        android:drawSelectorOnTop="false">
    </ListView>
    
    <TextView android:id="@id/android:empty"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:background="#FFff00"
        android:text="No data"
    />
    

    现在在您的主要活动中,像上面一样创建您的适配器。 像这样获取列表引用:-

    ListView list1 = (ListView)findViewById(R.id.list1);
    ListView list2 = (ListView)findViewById(R.id.list2);
    list1.setAdapter(adapter);
    list2.setAdapter(secondadapter);
    

    【讨论】:

    • 当我离开 android:id="@id/android:list" 进入第一个列表视图时,您的代码工作了
    • 但是我如何实现第二个 onItemListener 将重新发布编辑
    • 您可以为不同的列表视图编写数百个 itemclick 侦听器,就像您在上面为 list2 编写的一样
    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多