Cam Connor
Cam Connor

Reputation: 1231

Android - ListFragment OnClickListener is not working

I have a ListFragment in my android application, I have got it to work, but the OnClick Listener is not working, I tried just making it so that when any item on the list is selcted a Toast appears and it is not happening, there is no Error so I have no LogCat to post

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View v = inflater.inflate(R.layout.main, container, false);

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();


//...

    ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost });

    setListAdapter(adapter);

    ListView lv = (ListView)v.findViewById(android.R.id.list);

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem

            Toast.makeText(getActivity().getApplicationContext(), "Not Configured",
                    Toast.LENGTH_SHORT).show();

        }
    });

    return v;
}

Thanks

Upvotes: 2

Views: 6253

Answers (5)

Suresh Maidaragi
Suresh Maidaragi

Reputation: 2318

Make sure you

1.shouldn't have onclicklistener inside your Adapter

2.inside your XML layout

R.layout.main

Listview should be initialized like

 <ListView
    android:onClick="@id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/textHeader"
    android:layout_margin="5dp"
    android:divider="@color/DeepPink"
    android:dividerHeight="1sp"
    android:gravity="center"
    android:horizontalSpacing="1dp"
    android:visibility="visible" >

here id android:onClick="@id/list" important

  1. ListView should be initialize like ListView lv = getListView if your extending your fragment by ListFragment instead Fragment

Upvotes: 0

guardezi
guardezi

Reputation: 171

put

android:focusable="false"
android:clickable="false"

to all itens in your row

Upvotes: 0

S.Thiongane
S.Thiongane

Reputation: 6915

The ListFragment subclass already has it's overriden onListItemClick method.

The doc says:

This method will be called when an item in the list is selected. Subclasses should override

So there is no need to declare another listner for your listview.

Upvotes: 2

jirka
jirka

Reputation: 111

if your class extends ListFragment than everything you need to do is just overriding its onListItemClick method.

@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
  super.onListItemClick(l, v, pos, id);
  Toast.makeText(getActivity(), "Item " + pos + " was clicked", Toast.LENGTH_SHORT).show();
}

Upvotes: 7

Arvind V.
Arvind V.

Reputation: 15

Removing .getApplicationContext() should work. I have some code similar to yours from an app I made. Its from inside a fragment as well though works without problem. The db.remove is probably irrelevant to your code though because this code was written for an app with a database. Also maybe try changing new OnItemClickListener to new AdapterView.OnItemClickListener

    listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            db.remove( (int) l);
            Toast.makeText(getActivity(), "Item Deleted", Toast.LENGTH_LONG).show();
        }
    });

If that doesn't work, maybe try making a Context instance variable like:

private Context ctx = getActivity();

or

private final Context ctx = getActivity();

I have never worked with ListFragments before though, so I am not sure if anything I wrote will work.

Upvotes: 0

Related Questions