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 9310433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:06:40+00:00 2026-06-19T01:06:40+00:00

I have an activity to populate a listview. I have created the listview inside

  • 0

I have an activity to populate a listview. I have created the listview inside the onCreate method of the activity. Now I want to implement an onclick listner for the list. How to achieve this?

This is my activity class with the listview:

public class PollStationActivity extends Activity {



    static String response_str=null;
    static String response_code=null;

    String ac_code;
    String ac_id;
    String ac_lat;
    String ac_lon;
    String ac_name;

    // Hashmap for ListView
            ArrayList<HashMap<String, String>> PSList = new ArrayList<HashMap<String, String>>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // no more this
         setContentView(R.layout.activity_poll_station);

         //sendPostRequest();

         //Log.i("I am here","1");

            final String status_code_from_prev= getIntent().getExtras().getString("status");

            //Log.i("ss",status_code_from_prev);

            ArrayList<PSItem> PSList = new ArrayList<PSItem>();


            DatabaseHelper db = new DatabaseHelper(PollStationActivity.this);
            ContentValues values = new ContentValues();

             try {
              db.createDataBase();

              PSList = db.select(status_code_from_prev); 

              //Log.i("Count : ", " " + PSList.isEmpty());

             } catch (IOException e) {
              e.printStackTrace();
               }

             db.close();


         String[] fromColumns = {"name"};
            int[] toViews = {R.id.list_label_name}; // The TextView in simple_list_item_1

            ListView listView1 = (ListView) findViewById(R.id.poll_list_listView);  
            List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

            Iterator<PSItem> i = PSList.iterator();

            while(i.hasNext())
            {
                HashMap<String, String> map = new HashMap<String, String>();
                PSItem objPSItem = i.next();

                map.put("name", objPSItem.PS_NAME);
                map.put("ps_Id", objPSItem.PS_ID);
                fillMaps.add(map);
            }





            // fill in the grid_item layout
            SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_layout, fromColumns, toViews);
            listView1.setAdapter(adapter);

    }
}

Where should I put the listview onclick listner?

  • 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-19T01:06:41+00:00Added an answer on June 19, 2026 at 1:06 am

    You can set the listener right after you’ve initialized, for instance:

    ListView listView1 = (ListView) findViewById(R.id.poll_list_listView); 
        listView1.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                //Handle the click
            }           
        });
    

    Even though you have not set the adapter for the list yet, meaning there are no items displayed, it doesn’t matter where you set it, as long as you have already initialized the listview via findViewById

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Activity with ListView inside it and in the onCreate method of the
I have an activity that extends ListView. I populate my list with the results
I have an activity in whose onCreate method an Init function is called (the
I have created a Custom Array Adapter to populate the List View and when
I have a listview NoteList which contains a method doListRefresh() to select list contents
I am building an activity which I want to populate with contacts that have
I have created a custom Base Adapter class, to populate a list view with
I have an Activity that makes a remote server call and tries to populate
in my app i have activity with XML layout, i want to build an
I have a ListView with a custom list adapter (that extends BaseAdapter). My list

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.