0

I have a listview binded to an arrayadapter of strings... How do i set different clicklistener on each item in the list view

2 Answers2

0
  1. Register an item click listener on your ListView with http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
  2. In your AdapterView.OnItemClickListener#onItemClick you will get the view and an id.

You can use the id if you have a given order in your arraylist, or you could use View#findViewById and get the content of the view to figure out which item it is.

That means, you would not set a listener per item, rather you would have one listener for all items, and then do different things based on which item it was.

Erik Živković
  • 4,867
  • 2
  • 35
  • 53
  • Ok thanks... But can you gimme an example like a sample code or something? – Ayomide Aro Nov 01 '15 at 20:17
  • @AyomideAro here on StackOverflow you will always get more help if your question is more specific. If you edit your question and fill in your own code we can help you fill in the blanks. – Erik Živković Nov 01 '15 at 20:24
0

I think you could use it like this.

    adapter = new ArrayAdapter(getContext(), R.layout.list_item_forecast, R.id.list_item_forecast_textView, new ArrayList<String>());
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //what ever you want to give on item click
        }
    });

here by checking the position inside onItemClick you can assign specific tasks.

Nabeel K
  • 5,938
  • 11
  • 38
  • 68