'onItemClick and Toast don't work in a listfragment
i tried to do a simple Toast into my onitemclickmethod but nothing it's happening and no error just nothing happen when i press an item of the list
My listfragment :
public class F1_fr extends ListFragment {
View rootview;
TextView textView1;
ArrayAdapter<String> aa;
ArrayList<String> arrayList = new ArrayList<String>();
SQLiteDatabase db;
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootview=inflater.inflate(R.layout.f1_lay,container,false);
textView1=(TextView)rootview.findViewById(R.id.textView1);
db = getActivity().openOrCreateDatabase("testDB2", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS test2(mac VARCHAR,mdp VARCHAR,obj VARCHAR);");
aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayList);
setListAdapter(aa);
((ListView) rootview.findViewById(android.R.id.list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"lol",Toast.LENGTH_LONG).show();
}
});
Cursor cursor = db.rawQuery("SELECT * FROM test2", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if(cursor.moveToFirst())
{
do {
arrayList.add(cursor.getString(2));
} while (cursor.moveToNext());
}
rootview.findViewById(R.id.semi_transparent).
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
Intent intent = new Intent(getActivity(), ajout.class);
startActivityForResult(intent, 2);
}
}
);
return rootview;
}
and my layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@id/android:list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:text="" />
<com.getbase.floatingactionbutton.AddFloatingActionButton
android:id="@+id/semi_transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_plusIconColor="@color/primaryColorDark"
fab:fab_colorNormal="@color/primaryColor"
fab:fab_colorPressed="@color/AccentColor"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
So i'am just asking your help because i have absolutly no error nothing in the logcat just nothing is happening
Solution 1:[1]
You've implemented ListFragment
so directly @Override onListItemClick(...)
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something
Toast.makeText(getActivity(), "Lol", Toast.LENGTH_SHORT).show();
}
Take a look at this
Solution 2:[2]
I am not sure but try to put below line in RelativeLayout.
android:descendantFocusability="blocksDescendants"
Hope it will work.!!!
Solution 3:[3]
you have to override onListItemClick() of listFragment you dont call listview.setOnItemClick();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | M D |
Solution 2 | Ganesh Katikar |
Solution 3 | KomalG |