ArrayList<HashMap<String, String>> MemberList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> item = new HashMap<String, String>();
item.put("asdf", "asdf");
MemberList.add(item);
item.put("ffff", "ffff");
MemberList.add(item);
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, MemberList, android.R.layout.simple_list_item_2, new String[] { "Name", "Level" },
new int[] { android.R.id.text1, android.R.id.text2 });
list.setAdapter(simpleAdapter);
这是我的java代码。
,但.... 只有两个空白
ListView中没有文字
我该怎么办?
答案 0 :(得分:0)
更改为
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, MemberList, android.R.layout.simple_list_item_2, new String[] { "asdf", "ffff" },
new int[] { android.R.id.text1, android.R.id.text2 });
你有
item.put("asdf", "asdf");
item.put("ffff", "ffff");
所以你需要
new String[] { "asdf", "ffff" },
查看构造函数
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
Added in API level 1
Constructor
Parameters
context The context where the View associated with this SimpleAdapter is running
data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
from A list of column names that will be added to the Map associated with each item.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
答案 1 :(得分:0)
以下是解决方案:
ListView list = (ListView) findViewById(R.id.list);
ArrayList<String> arr_adt = new ArrayList<String>();
HashMap<String, String> item = new HashMap<String, String>();
ArrayList<HashMap<String, String>> MemberList = new ArrayList<HashMap<String, String>>();
item.put("asdf", "asdf");
MemberList.add(item);
item.put("ffff", "ffff");
MemberList.add(item);
Toast.makeText(getApplicationContext(), MemberList.toString(),
Toast.LENGTH_LONG).show();
Log.i("MAP", MemberList.toString());
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
MemberList, android.R.layout.simple_list_item_2,
new String[] {"asdf","ffff"}, new int[] {android.R.id.text1,android.R.id.text2});
list.setAdapter(simpleAdapter);
希望这会有所帮助.. :)