33

I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/ but I can't seem to add an onclicklistener I just want to be able to do an action when I click on the row, with the data that the row contains of course any ideas? thanks

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.liste);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Setup the list view
    final ListView prestListView = (ListView) findViewById(R.id.list);
    final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);

    prestListView.setAdapter(prestationAdapterEco);

    // Populate the list, through the adapter
    for(final prestationEco entry : getPrestations()) {
        prestationAdapterEco.add(entry);
    }
    prestListView.setClickable(true);
    prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Object o = prestListView.getItemAtPosition(position);
            String str=(String)o;//As you are using Default String Adapter
            Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
        }
    });
}
Ruan_Lopes
  • 1,381
  • 13
  • 18
Mike Bryant
  • 2,455
  • 9
  • 39
  • 60

6 Answers6

60
listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object o = prestListView.getItemAtPosition(position);
        prestationEco str = (prestationEco)o; //As you are using Default String Adapter
        Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
    }
});
spuente
  • 314
  • 5
  • 16
Maulik J
  • 2,745
  • 19
  • 22
  • 1
    that's exactly what I tried, how would I get the item's details? – Mike Bryant Feb 01 '12 at 14:49
  • I always add a vector into my custom listview and to get data i create a new object, eg Object obj = (Object)Vector.get(position); – Maulik J Feb 01 '12 at 14:54
  • i have created a custom list adapter wherin i pass the vector to the class and it creates a listview with the total vector size and data in it... – Maulik J Feb 01 '12 at 15:08
9

If your Activity extends ListActivity, you can simply override the OnListItemClick() method like so:

/** {@inheritDoc} */
@Override  
protected void onListItemClick(ListView l, View v, int pos, long id) {  
    super.onListItemClick(l, v, pos, id);

    // TODO : Logic
}  
Jean-Philippe Roy
  • 4,752
  • 3
  • 27
  • 41
  • the listview just extends activity not listactivity, http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/ – Mike Bryant Feb 01 '12 at 14:50
  • For me in a ListFragment this was the ONLY way to get onClick to work. setOnItemClickListener just doesn't work. I guess the same would be true for ListActivity. See http://stackoverflow.com/questions/14080332/listfragment-onitemclicklistener-not-working – Dale Apr 28 '15 at 21:22
1
list.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
r-magalhaes
  • 427
  • 2
  • 9
  • 18
1

The prestListView.getItemAtPosition(position); returns the UI widget: Text, Icon, ...

Try this instead:

Object o = prestationAdapterEco.getItemAtPosition(position);

or

Object o = arg0.getItemAtPosition(position);

Get the object from the adapter. Not from the list-view.

2. Object o is a prestationEco object. Not a String.

Blehi
  • 1,990
  • 1
  • 18
  • 20
0

Try this:

    list.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3)
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
});
mixel
  • 25,177
  • 13
  • 126
  • 165
0

You are doing

Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter

The o that you get back is not a String, but a prestationEco so you get a CCE when doing the (String)o

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119