2

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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();
  }
} );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.