0

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");
    }
Community
  • 1
  • 1
Arrigo Pierotti
  • 203
  • 4
  • 10
  • 18
  • Try this; Change onListItemClick To onItemClick – Want2bExpert Sep 04 '15 at 16:36
  • @Arrigo Pierotti...override it – Jerry Sep 04 '15 at 16:40
  • I removed some extra stuff needed for implementing `onItemClick` instead of `onListItemClick` and the `@Override` was only missing when I copyed and pasted here. – Arrigo Pierotti Sep 04 '15 at 16:45
  • can you post your layout? – Jerry Sep 04 '15 at 17:02
  • There is really no general layout since this fragment is inside a `FragmentTabHost` and the layout of a single element (with `android:clickable=true` in the root element) is just 2 text views, one image and an image button – Arrigo Pierotti Sep 04 '15 at 17:06
  • 1
    I found one thread in SO where they had similar problems where the components of the layout were stealing the input from other components. Check this, it might help you [http://stackoverflow.com/questions/7274231/listfragment-onlistitemclick-not-being-called] – Jerry Sep 04 '15 at 17:11
  • Thanks @JerrySangma! The ImageButton was "stealing" my input: changed to a normal ImageView made all the things work! – Arrigo Pierotti Sep 04 '15 at 17:17
  • 1
    @Arrigo Pierotti welcome...you can now close the discussion by accepting the answer....I edited my previous answer below. Happy Coding – Jerry Sep 04 '15 at 17:22

3 Answers3

1

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

Jerry
  • 410
  • 6
  • 17
0

Add the super constructor and override it:

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

EDIT: try this code

public class FragmentD extends ListFragment {

private SQLiteDatabase db;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(YOUR LISTFRAGMENT LAYOUT HERE, container, false);

    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);

    return v;
}

@Override
public void onListItemClick (ListView l, View v, int pos, long id) {
    super.onListItemClick(l, v, pos, id);
    Log.d("CLICK", "pressed");
}
user3641702
  • 393
  • 1
  • 5
  • 18
  • I removed the implement and the getListView since they where for `onItemClick` and the @override was missing only here (i had to do a little edit when i copyed and pasted here) but nothing has changed, still no sign in the Log... – Arrigo Pierotti Sep 04 '15 at 16:42
  • You forgot the super constructor and remove this -> implements AdapterView.OnItemClickListener – user3641702 Sep 04 '15 at 16:46
  • The super constructor has been added (my bad for not adding it in the code at the beginning) and still not works – Arrigo Pierotti Sep 04 '15 at 16:50
  • Remove the implements AdapterView.OnItemClickListener. Any ListFragment needs only to override the onListItemClick method to make it work – user3641702 Sep 04 '15 at 16:52
  • Sorry for all the things I forget to change in the code up here, but even without this implement it's still not working – Arrigo Pierotti Sep 04 '15 at 16:54
0

I have got two solution

  1. Simply Override onListItemClick

    @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id);

      Log.i("position",position);
    

    }

  2. On the root layout. Add this code

    android:descendantFocusability="blocksDescendants"

Hope it helps

Ajay Shrestha
  • 2,433
  • 1
  • 21
  • 25
  • I removed some extra stuff needed for implementing `onItemClick` instead of `onListItemClick` and the `@Override` was only missing when I copyed and pasted here – Arrigo Pierotti Sep 04 '15 at 16:46