Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8021543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:01:05+00:00 2026-06-04T22:01:05+00:00

I like to pass selected item value from ListView to another activity.I am using

  • 0

I like to pass selected item value from ListView to another activity.I am using City class with atributes idCity and nameCity and I like to pass the value of the city that is selected to another activity
This code is throwing java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap

public ListView cities;
ArrayList<City> cityList=new ArrayList<City>();

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cities_view);
    Resources r = getResources();
    String[] c = r.getStringArray(R.array.cities);


    for(int i=0;i<c.length;i++)
    {
        cityList.add(new City(i,c[i]));
    }
    for(int i=0;i<c.length;i++)
    {
        System.out.println(cityList.get(i).idCity);
        System.out.println(cityList.get(i).nameCity);
    }

    cities = (ListView) findViewById(R.id.listCities);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, c);
    cities.setAdapter(adapter);



    File file = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath(), getString(R.string.exchangeOfficesFile));

    if (!file.exists()) {
        startService(new Intent(this, DownloadAsyncService.class));
    }


try{
    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    for (City c : cityList) {
        Map<String, String> map = new HashMap<String, String>(2);
        map.put("idCity", Integer.toString(c.getIdCity()));
        map.put("nameCity", c.getNameCity());

        data.add(map);
    }


    cities.setOnItemClickListener(new OnItemClickListener() {


        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(getApplicationContext(),
                    CitiesDetails.class);

            HashMap<String,String> map=(HashMap<String, String>) parent.getItemAtPosition(position);

            intent.putExtra("idCity", map.get("idCity"));

            intent.putExtra("nameCity", map.get("nameCity"));



            startActivity(intent);
        }

    });

}catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}
}

Maybe somebody had the same problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:01:06+00:00Added an answer on June 4, 2026 at 10:01 pm

    Your array adapter is a list of strings:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, c);
    

    getItemAtPosition(int position) will return an Object that matches the type of the Adapter. So in this case it will return a string, but you are trying to cast it to a HashMap<String, String>

    HashMap<String,String> map=(HashMap<String, String>) parent.getItemAtPosition(position);
    

    Change it to:

    String result = (String) parent.getItemAtPosition(position);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't seem to get the value for a 'selected item' to pass to
seems like I'm stuck with jQuery tabs. I'm trying to pass selected tab name
I would like to pass a MySQL Datetime value like below in a site
I would like to pass values into the constructor on the class that implements
I'm trying to pass a value from an android spinner selection into a url.
Given the following I would like to keep the currently selected item selected on
In Iphone i am able to pass selector id like this way CCMenuItemSprite* item
I have a select list like so: <select id=drpColours> <option value= selected=selected>Choose color</option> <option
I would like to pass the environment variable to the glassfish domain.xml as the
I would like to pass the user id of the person who started a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.