0

I have my adaptor class with getView method as follows:

public View getView(int position, View convertView, ViewGroup parent) {
 View view = null;
 if (convertView == null) {
   LayoutInflater inflator = context.getLayoutInflater();
   view = inflator.inflate(R.layout.userlist, null);
   final ViewHolder viewHolder = new ViewHolder();
   viewHolder.text2 = (TextView) view.findViewById(R.id.stuname);
   viewHolder.text = (TextView) view.findViewById(R.id.rollno);
   viewHolder.checkbox = (CheckBox) view.findViewById(R.id.cb);
   viewHolder.checkbox
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

         @Override
         public void onCheckedChanged(CompoundButton buttonView,
             boolean isChecked) {
           Model element = (Model) viewHolder.checkbox
               .getTag();
           element.setSelected(buttonView.isChecked());
          // Toast.makeText(getContext(), "Checked", 
                  //  Toast.LENGTH_SHORT).show();


         }
       });
   view.setTag(viewHolder);
   viewHolder.checkbox.setTag(list.get(position));
 } else {
   view = convertView;
   ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
 }
 ViewHolder holder = (ViewHolder) view.getTag();
 holder.text.setText(list.get(position).getName());
 holder.text2.setText(list.get(position).getRoll());
 holder.checkbox.setChecked(list.get(position).isSelected());
 return view;
}

There is an oncheckedchange listener here.

My question is how to pass the information like which list item has changed to the my main activity? Also how in the main activity can we perform some action when the checkbox state has changed?

2 Answers 2

1

One idea is to call CheckBox.setTag(), tagging it with the data that you want to hold and having the onCheckedChange listener use CheckBox.getTag() to retrieve the data. You could even do this with onClick via XML.

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

1 Comment

That worked brilliantly. I used onClick on xml to call the onchecked method in my main activity and the getTag() method inside the onchecked method to check the status of the checkbox. THNX A LOT! :)
1

You can check this example

http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/

That has how to use selection checkboxes in a listview and process on click actions

You may also find this helpful on how to make clickable zones such a checkboxes etc since listviews don't actually allow you to check the checkbox itself directly

http://wiresareobsolete.com/wordpress/2011/08/clickable-zones-in-listview-items/

4 Comments

i want to put a oncheckedchecked listner inside the main actvity can i do this ?
What you could do is declare a static method in the main activity then, in the oncheckedlistener in the listview, call the main activity's static custom onchecked listener method
But in the static method i could not manipulate any non static variables of the main activity.
How many of this main class are you instantiating? If only one at a time, as I would imagine is the case with an activity, there is no harm in having things static?

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.