Yaya
Yaya

Reputation: 620

Android listfragment onItemClick not working ?

I have a ListFragment, and i want to use onitemclick event. But i have many problems. can you help me ? I put event onstart is this right or correct?

Thanks in advance. ..

  public class MesajFragment extends android.support.v4.app.ListFragment
   ...
  @Override
public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    getListView().setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), arg2 + " okwww ",
                    Toast.LENGTH_LONG);

        }
    });
}

Upvotes: 3

Views: 4773

Answers (2)

cwc
cwc

Reputation: 10395

Your example gets the ListView and sets its click listener. When extending ListFragment, you need to override onListItemClick instead:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Handle item click
}

Upvotes: 5

dymmeh
dymmeh

Reputation: 22306

You aren't calling .show() on your toast message. It should look like this:

Toast.makeText(getActivity(), arg2 + " okwww ",
                    Toast.LENGTH_LONG).show();

See the .show() at the end? Your onClick is likely working. It just doesn't appear to be since your Toast message isn't showing

Upvotes: 9

Related Questions