I am using a custom Adapter
like below and that works fine to actually display the data in my ListFragment
normally:
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<String> data;
private static LayoutInflater inflater = null;
public CustomAdapter(Context context, ArrayList<String> data) {
// TODO Auto-generated constructor stub
this.context = context;
this.data = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.custom_row, null);
TextView textView = (TextView) vi.findViewById(R.id.textView);
textView.setText(data.get(position));
CheckBox checkBox = (CheckBox) vi.findViewById(R.id.checkBox);
checkBox.setChecked(true);
return vi;
}
}
My issue comes from when I want to utilise the onListItemClick
method in my ListFragment
, it simply never gets called:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "Position: " + position, Toast.LENGTH_SHORT).show();
}
However I have found that if I just use a normal ArrayAdapter
that I don't get any issues:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data);
setListAdapter(arrayAdapter);
}
Although this is simply not feasible to use as I need the custom layout file to be implemented and therefore I wanted to use my custom Adapter
like so:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
CustomAdapter adapter = new CustomAdapter(getActivity(), data);
setListAdapter(adapter);
}
However as soon as I do, the clicking on the list row completely stops working. Please can someone give me some guidance as to why the onListItemClick
method does not get called properly when I use my own custom Adapter
?
Based on your getView()
method from the adapter, I see that you have a CheckBox
in the list item. You need to add android:focusable="false"
to it (as a matter of fact you need to do it to any control that is focusable by default).