Search code examples
javaandroidandroid-intentarraylistsimpleadapter

Send SimpleAdapter or ArrayList<Object> with Intent


I have codes like this. This is the first class which is the source one.

List<HashMap<String, Object>> sonucList = new ArrayList<HashMap<String,Object>>();

HashMap<String, Object> searchHM = new HashMap<String, Object>();
searchHM.put("Name", eslenenName);
searchHM.put("image", R.drawable.appicon);
sonucList.add(searchHM);

String[] from = { "Name","image"};
int[] to = { R.id.name,R.id.imageView1};

SimpleAdapter adapterSearch = new SimpleAdapter(getBaseContext(), sonucList, R.layout.list, from, to);

The code is like this. I am creating a HashMap, putting some values more in my original code. I am adding these hashmaps to ArrayList with a for loop and creating a SimpleAdapter with this list.

What I have to do is that, send this ArrayList or SimpleAdapter to a new class. I want to create a same listview with this adapter in another page. I tried with intent.putExtra() but putExtra doesn't accept Object it said.

What can I do? Thanks.


Solution

  • The compiler knows that HaspMap is Serializable because you're referring that Collection via its specific type. But it doesn't know that your List is Serializable because you're referring this Collection through it's specific type. There are three possibilities.

    1. Use a cast, use intent.putExtra((Serializable) sonucList);
    2. Use a specific class, declared it like this: ArrayList<HashMap<String, Object>> sonucList = ...;
    3. Create a type parameter that implements both, List and Serializable, i.e. <SerializableList extends List & Serializable>, and use SerialiableList instead of List.

    By the way, if you're dealing with Android, you might want to replace Object with Serializable in many places in your code to avoid NotSerializableException at compile time level instead of getting them at runtime.