问题
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");
}
回答1:
Edited Answer: Maybe the components of the layout were stealing the input from other components of the layout
回答2:
I have got two solution
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);
}
On the root layout. Add this code
android:descendantFocusability="blocksDescendants"
Hope it helps
回答3:
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");
}
来源:https://stackoverflow.com/questions/32402628/adding-onlistitemlick-onitemlongclick-in-a-listfragment