Soyeon Jeon
Soyeon Jeon

Reputation: 21

android listview checkbox event

i'm trying to implement custom listview. however, i can't get the click event for my checkbox;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final int pos = position;
    View view = convertView;

    if(view == null){
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.listview,parent, false);
    }
    TextView textview = (TextView)view.findViewById(R.id.listview_text);
    final CheckBox checkbox = (CheckBox)view.findViewById(R.id.listview_check);
    checkbox.setFocusable(false);
    checkbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, pos+"", Toast.LENGTH_SHORT).show();
        }
    });

    return super.getView(position, convertView, parent);
}

I want that if I clicked checkbox in ListView, pop the Toast about row position. But, this code run just check box click event just 0 row. checkbox in other row don't trigger event.

how can i fix it?

Upvotes: 2

Views: 61

Answers (2)

Sush
Sush

Reputation: 3874

Onclicklisteners on check boxes wont work.

usually check box click listeners should be like this

CheckBox chkbbx = (CheckBox) findViewById(R.id.chkbbx);
chkbbx.setOnCheckedChangeListener( new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
  }
} );

Upvotes: 0

Ramanlfc
Ramanlfc

Reputation: 8354

the last line in getView() seems suspect. you should be returning the view you created or convertView passed to you.

Upvotes: 1

Related Questions