Reputation: 491
I'm trying to create a very basic ListView dialog, where each item is to have a sub-heading. For this purpose I'm trying to use the simple_list_item_2 layout with an adapter for a List(Map(String,String)). The code is as follows:
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();
}
}
When executed, the result looks like this:
http://i.imgur.com/8gkTECe.png
As you can see, only one of the four text strings are displayed. What's going on here?
Upvotes: 1
Views: 1268
Reputation: 6438
you are setting values on datum instead of datum2.
// 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"); //should be datum2
datum.put("address", "CAN"); //should be datum2
data.add(datum2);
Upvotes: 1