this is my code:
ListView:
ListView lv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.aller_layout, container, false);
lv = (ListView) rootView.findViewById(R.id.listView1);
CustomAdapter ca = new CustomAdapter(rootView.getContext(), R.layout.preflist_checkbox_layout, oggetti);
lv.setAdapter(ca);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id){
Log.i("onclick", "si");
Toast.makeText(view.getContext(), "You clicked", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
and the adapter:
public class CustomAdapter extends ArrayAdapter<Oggetto>{
private Context context;
private Oggetto[] oggetti = null;
private int layoutid;
public CustomAdapter(Context context, int layoutid, Oggetto[] oggetti){
super(context, layoutid, oggetti);
this.context = context;
this.oggetti = oggetti;
this.layoutid = layoutid;
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutid, parent, false);
}
Oggetto oggetto = oggetti[position];
CheckBox cb = (CheckBox) convertView.findViewById(R.id.pref_checkbox);
cb.setText(oggetto.nome);
cb.setChecked(oggetto.preferito == 1);
return convertView;
}
}
right now I'm just trying to catch the click on the checkbox in the list view but the code it's not working as it should and neither the Log or the Toast are showed when i click on any element.
Where I got it wrong?
EDIT: since seems to be a problem in the xml I'll add the ListView row's layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/pref_checkbox"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>