我想用ListView
填写一些数据,然后使用SimpleAdapter
。但我认为SimpleAdapter
仅适用于List<HashMap<String, String>>
,我List<Object>
喜欢跟随代码:
我该怎么办? 有什么办法吗?
List<DSarchive> programs = ...;
String[] from = new String[] {"name", "time", "day", "month"};
int[] to = new int[] { R.id.text1, R.id.text2, R.id.text3, R.id.text4};
SimpleAdapter adapter = new SimpleAdapter(getActivity(), programs, R.layout.my_list_row, from, to);
// ^ how can I use this "programs" List???
lv.setAdapter(adapter);
我想在List
中使用适配器MyObject
。
MyObject 类:
public class MyObject{
public ArrayList<String> links;
public String name;
public List<HashMap<String, String>> adapter;
.
.
.
}
答案 0 :(得分:4)
尝试使用ArrayAdapter
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("temp1");
arrayList.add("temp2");
ListView listView = new ListView(getActivity());
listView.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, arrayList));
编辑
SimpleListAdapter
不仅限于使用Strings
。它确实需要一个String
键,但它可以映射到任何对象。例如,我使用以下代码创建了一个ListView
,其中包含一个支持TextViews
的图标。希望这会有所帮助。
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
Map<String, Object> dataMap = new HashMap<String, Object>(3);
dataMap.put("Item1", dataObject); //icon
dataMap.put("Item2", dataString);
dataMap.put("Item3", dataString2);
data.add(dataMap);
dataMap = new HashMap<String, Object>(2);
dataMap.put("Item1", dataObject); //icon
dataMap.put("Item2", dataString);
dataMap.put("Item3", dataString2);
data.add(dataMap);
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(),
(List<? extends Map<String, ?>>) data,
R.layout.layoutFile2,
new String[] {"Item1", "Item2", "Item3"} ,
new int[] {R.id.item1, R.id.item2, R.id.item3}){
//overload the getChildView or any other Override methods
};