-1

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>
Higure
  • 235
  • 1
  • 5
  • 19

4 Answers4

0

I am guessing CheckBox takes focus when you click on ListItem

Have this

android:descendantFocusability="blocksDescendants"

for the root element in preflist_checkbox_layout.xml

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Still not working, I edited the post with the xml code. – Higure May 23 '14 at 16:53
  • @Higure i don't see any reason why it won't work. You don't require this `android:focusable="false" android:focusableInTouchMode="false"` – Raghunandan May 23 '14 at 16:55
  • I've taken down those 2 lines and it's still not working, I click but neither the Log or the Toast shows up. – Higure May 23 '14 at 17:05
0

Add these properties to your CheckBox:

android:focusable="false"
android:focusableInTouchMode="false"
CanDroid
  • 633
  • 6
  • 15
0
 lv.setOnItemClickListener(this);
    @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Toast toast = Toast.makeText(getApplicationContext(),
                    "Item " + (position + 1) + ": ",Toast.LENGTH_SHORT);

        }

Try this

Meghna
  • 539
  • 4
  • 14
  • what difference would this make? – Raghunandan May 23 '14 at 17:51
  • there no big difference,he has separate class for onItemClickListen,i just added within same class.because i saw other solution.those solution should work.but not working.so it might be help. – Meghna May 23 '14 at 17:54
  • this makes no difference at all. op has annonymous inner class. It will work – Raghunandan May 23 '14 at 17:58
  • It shouldn't make any difference, moreover, I'm working inside a fragment and not an activity so `lv.setOnItemClickListener(this);` gives and error. – Higure May 23 '14 at 18:09
  • instead of this u can use getContext(),anyway as RAghunandan said it wont make any difference. – Meghna May 23 '14 at 18:11
  • you can put toast inside ArrayAdapter class onclcik of checkbox – Meghna May 23 '14 at 18:25
0

Ok, i managed to find the problem.

I couldn't make the onItemClick works, I suppose that is because it actually don't works with checkboxes at all (or at least i couldn't manage to find a way).

Therefore I linked a onClickListener to my checkbox inside the adapter and it works like a charm.

Higure
  • 235
  • 1
  • 5
  • 19
  • no this is wrong. Even with checkbox you can click the list item and check the check box. – Raghunandan May 26 '14 at 10:18
  • for a example check this http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button. Wrong answer accepted – Raghunandan May 26 '14 at 10:20