我尝试使用简单的适配器和散列映射在一个活动中使用multilpe listviews
这是我的一些代码......
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);
}
和itemListener
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文件中声明第二个或更多listviews。最后我想要一个带有scrollview的布局,其中包含textview然后是listview,然后是另一个textview和另一个listview ....任何帮助都会很棒
这里编辑的答案几乎都是他们的。但我需要onItemListener for list2和list3
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的任何想法?
答案 0 :(得分: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);