android - 如何将数据从对象输入到 SimpleAdapter

标签 android listview android-arrayadapter simpleadapter

我想填写我的 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 .

MyObject 类:

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

最佳答案

尝试使用 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
                                         };

关于android - 如何将数据从对象输入到 SimpleAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748356/

相关文章:

java - 从拨号盘调用号码返回警告 : "Call requires permission which may be rejected by user"

android - 涟漪效应在 ListView 中不起作用

android - kotlin android proguard错误

android - arrayadapter.clear() 删除底层数据

java - Android使用适配器将两个不同类型的数组列表显示到一个无缝 ListView 中

android - 跨文本上的 URLSpan 未在绘制的 StaticLayout 中显示

java - 解析 JSON 数据并填充 ListView

c# - 将字符串列表存储在 ListView 的单独列中 (C#)

java - 如何清除ListView,它只是在Android App中的旧内容之后添加新内容?

java - 如何在 Android 上的 Spinner 中使用 itemClickListener