我正在尝试创建一个非常基本的 ListView 对话框,其中每个项目都有一个子标题。为此,我尝试将 simple_list_item_2 布局与 List(Map(String,String)) 的适配器一起使用。代码如下:
public static class StoreList extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final List<Map<String, String>> data = new ArrayList<Map<String, String>>();
// Dummy data
Map<String, String> datum = new HashMap<String, String>();
datum.put("name", "Name1");
datum.put("address", "USA");
data.add(datum);
Map<String, String> datum2 = new HashMap<String, String>();
datum.put("name", "Name2");
datum.put("address", "CAN");
data.add(datum2);
// Adapter for the ListView:
SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
data,
android.R.layout.simple_list_item_2,
new String[] {"name", "address"},
new int[] {android.R.id.text1, android.R.id.text2});
// Listener for the ListView:
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(getActivity(), StoreActivity.class);
Map<String, String> listItem = data.get(which);
i.putExtra("name", listItem.get("name"));
i.putExtra("address", listItem.get("address"));
startActivity(i);
}
};
// Build the ListView dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.storeListTitle);
builder.setAdapter(adapter, listener);
return builder.create();
}
}
执行时,结果如下所示:
http://i.imgur.com/8gkTECe.png
如您所见,仅显示四个文本字符串之一。这里发生了什么?