Search code examples
androidandroid-listfragmentsimplecursoradapteronitemclicklistener

Adding onListItemLick/onItemLongClick in a ListFragment


SOLVED IN A COMMENT: Adding onListItemLick/onItemLongClick in a ListFragment The problem was generated from an ImageButton in the layout of the single element in the ListFragment that was stealing the input even with the focusable element set to false, so i had to remove it from the layout.

I've got a ListFragment populated by an extended class of SimpleCursorAdapter (mainly for overriding the newView method) but I wanted to add an AlertDialog when the user presses (or long presses) an item in the list the adapter generated. I've tryed both onListItemClick and onItemLongClick with a simple log write using Log.d method but nothing happens in both cases and i don't know where is the problem since the code is really simple:

public class FragmentD extends ListFragment {

private SQLiteDatabase db;

@Override
public void onActivityCreated (Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    myDatabase myDBHelper = new myDatabase(getActivity());
    db = myDBHelper.getWritableDatabase();

    Log.d("DB", "Insert fatto");

    String[] res_columns = new String[] {myDatabase.COLUMN2, myDatabase.COLUMN2,};
    String sortOrder = myDatabase.COLUMN1 + " DESC";

    String where = "*";

    Cursor testCursor = db.rawQuery("select * from " + database.DATABASE_TABLE, null);

    myAdapter adapter = new myAdapter(getActivity(),
            R.layout.list_element,
            testCursor,
            res_columns,
            new int[] { },
            0);

    setListAdapter(adapter);
}

@Override
public void onListItemClick (ListView l, View v, int pos, long id) {
    super.onListItemClick(l, v, pos, id);
        Log.d("CLICK", "pressed");
    }

Solution

  • Edited Answer: Maybe the components of the layout were stealing the input from other components of the layout