androidlistviewandroid-arrayadaptersimpleadapter

how to input data from Object into SimpleAdapter


I want to fill out my ListView with some data and I use SimpleAdapter. But I think SimpleAdapter only works with List<HashMap<String, String>> and I've List<Object> like follow codes:

What can I do? Is there any way?

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);

I want to use adapter List in MyObject.

MyObject class:

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

Solution

  • Try using an 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));
    

    EDIT

    The SimpleListAdapter is not constrained to only use Strings. It does require a String key, but it can map to any object. For example, I used the following code to create a ListView that contains an Icon with two supporting TextViews. Hope this helps.

    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
                                             };