我正在使用这个简单的代码来填充包含两个文本视图的自定义行。这些行显然已经填充(因为Toast显示hashmap持有两个字符串值),但列表中的行显示为空白。这很奇怪。可能是什么原因?
public class TwoHeaded extends ListActivity {
ArrayList<Map<String, String>> doubleDamage = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
doubleDamage = buildData();
String[] from = {"platform","os"};
int[] to = {R.id.tvTwoPlatform, R.id.tvTwoOs};
SimpleAdapter adapter = new SimpleAdapter(this, doubleDamage,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData(){
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Android", "Jelly Bean"));
list.add(putData("Microsoft", "Windows"));
list.add(putData("Apple","Macintosh"));
return list;
}
private HashMap<String, String> putData(String platform, String os){
HashMap<String, String> item = new HashMap<String, String>();
item.put(platform, os);
return item;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(this, doubleDamage.get(position).toString(), Toast.LENGTH_SHORT).show();
}
}
这是包含两个文本视图的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvTwoPlatform"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp" >
<TextView
android:id="@+id/tvTwoPlatform"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
android:layout_marginTop="8dip"
android:text="text For Platform "
android:textAppearance="?android:attr/textAppearanceListItem" />
<TextView
android:id="@+id/tvTwoOs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/text1"
android:layout_below="@android:id/text1"
android:text="text For Os"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
答案 0 :(得分:1)
请参阅SimpleAdapter(上下文,数据,资源,从,到)文档。
它的内容如下:
所以列表中的每个条目都应该有“platform”和“os”字段,如下所示:
private HashMap<String, String> putData(String platform, String os){
HashMap<String, String> item = new HashMap<String, String>();
item.put("platform", platform);
item.put("os", os);
return item;
}
另外,如果你使用android.R.layout.simple_list_item_2作为资源,你的“to”“
int[] to = {R.id.tvTwoPlatform, R.id.tvTwoOs};
应该在layout.simple_list_item_2.xml文件中定义文本ID:
int[] to = {android.R.id.text1, android.R.id.text2};