Reputation: 301
I got a simple ListFragment in my app to show static datas like Strings. I need to make list items clickable with Single Choice. Here it is what i tried so far. I can see list items but i am not able to get any logs from clicking list items. What's wrong with that code ?
public class SettingsTab extends ListFragment implements OnItemClickListener{
String[] numbers={"one","two","three","four"};
ListView list ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Settings tab", "Fragment Created (onCreate)");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("Settings tab", "Fragment Created (onActivityCreate)");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.settingsfragment, container, false);
list = (ListView)v.findViewById(android.R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.listitemlayout,
R.id.listitemUniName, names));
list.setOnItemClickListener(this);
Log.d("Settings tab", "Fragment Created (onCreateView)");
return v;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getActivity().getBaseContext(), "Item clicked: " + arg2, Toast.LENGTH_LONG).show();
Log.d("settings", "click worked");
}
Upvotes: 0
Views: 4857
Reputation: 459
@Jukka Raanamo is right, this helped me, you can get rid of OnItemClickListener
and method onItemClick()
. Then override onListItemClick()
.
For example:
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// Do whatever you need to do here.
Toast.makeText(getActivity().getBaseContext(), "Item clicked: " + arg2, Toast.LENGTH_LONG).show();
Log.d("settings", "click worked");
}
Thanks @Jukka Raanamo
Upvotes: 1
Reputation: 451
ListFragment is already a listener so do not set OnItemClickListener but override this method instead: http://developer.android.com/reference/android/app/ListFragment.html#onListItemClick(android.widget.ListView, android.view.View, int, long).
Upvotes: 2