0

I am sending the arrayList from my main class which is listView to another class which displays details of individual items of the list.

 lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
 Intent intent = new Intent(ListActivity.this, DetailActivity.class);
 intent.putExtra("event",myList);
 startActivity(intent);
 }

Now in DetailActivity.class this is how I get the ArrayList. I can see all the values correctly when I print the arrayList.

 Intent i = getIntent();
 ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("event");
 System.out.println(myList);

Here's how I am trying to retrieve the first value and store it in a variable.

  for (int e=0;e<myList.size();e++)
    {
        String name = myList.get(0);
        System.out.println(name);
    }

I get java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String What am I doing wrong here?

3
  • What is the type of myList in the first snippet? Commented Mar 6, 2017 at 21:35
  • ArrayList<HashMap<String, String>> myList; Commented Mar 6, 2017 at 22:10
  • you canot cast HasMap to String. Commented Mar 6, 2017 at 23:19

1 Answer 1

2

In your first activity myList is Array of HashMap, and you try to cast it to String which cannot be done.

 ArrayList<HashMap<String, String>> myList = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("event");

This should be the correct way to convert it

Sign up to request clarification or add additional context in comments.

2 Comments

I need to iterate and store individual values in variables. Can you please guide me?
@smiley I dont know how your structure looks like and i expect you dont need Array of HashMap. But as this was question about the casting you can ask another one with your other problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.