1

我想用一些数据填写我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);

我想ListMyObject.

我的对象类:

public class MyObject{
    public ArrayList<String> links;
    public String name;
    public List<HashMap<String, String>> adapter;
    .
    .
    .
}
4

1 回答 1

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包含两个支持的 Icon 的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
                                         };
于 2013-07-19T14:31:58.223 回答