0

The image below is my listview with row item checkbox

enter image description here

when i select checkbox @row 1:

the id of that image is printed out using the loop below

final CheckBox cb = (CheckBox) row
                .findViewById(R.id.cbBox);
        cb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                if (infodata.get(position).isclicked) {
                    infodata.get(position).isclicked = false;
                } else {
                    infodata.get(position).isclicked = true;
                }

                for(int i=0;i<infodata.size();i++)
                {
                    if (infodata.get(i).isclicked) {

                        System.out.println("Selectes Are == "+data.get(i).getId());

                    }
                }
            }
        });

when i click on first checkbox result is:

Selectes Are == 1000000062

second checkbox click result are:

Selectes Are == 1000000062 Selectes Are == 1000000095

third checkbox click result are:

Selectes Are == 1000000062 Selectes Are == 1000000095 Selectes Are == 1000000058

Note that after each checkbox click it click all infodata selected again.

How can i stored selected values resulted from System.out.println("Selectes Are == "+data.get(i).getId()); into an array for e.g. [1000000062,1000000095,1000000058]

5
  • Add onclick code here? You can do it by using arrayList.. Commented Jan 13, 2014 at 7:46
  • i updated the question with the checkbox onclicklistener Commented Jan 13, 2014 at 7:50
  • What is purpose of infodata.get(position).isclicked check and setting to false? Commented Jan 13, 2014 at 7:52
  • 1
    May be you can perform these operations on a separate button click after all the selection is done. Commented Jan 13, 2014 at 7:55
  • yes but how to track selected click items Commented Jan 13, 2014 at 8:22

2 Answers 2

1

Please try this :

@Override
public void onClick(View v) {
  // TODO Auto-generated method stub
   ArrayList<String> selectedId = new ArrayList<String>();

   if (infodata.get(position).isclicked) {
         infodata.get(position).isclicked = false;
   } else {
         infodata.get(position).isclicked = true;
   }

   for(int i=0;i<infodata.size();i++) {
      if (infodata.get(i).isclicked) {
          selectedId.add(data.get(i).getId());    
      }
   }
   System.out.println("Selected id are : "+selectedId.toArray());
}
Sign up to request clarification or add additional context in comments.

2 Comments

selectedId.toArray() returns an object not an array
"Returns a new array containing all elements contained in this ArrayList"
0

Create and empty ArrayList outside of the onClick() method.

Use this code:

if (infodata.get(position).isclicked) {
   infodata.get(position).isclicked = false;
   //Remove it if you want to
   //arrayList.remove(data.get(i).getId());
} else {
   infodata.get(position).isclicked = true;
   arrayList.add(data.get(i).getId());
}

You can remove the for loop as it may not be needed.

Edited:

In the end put for loop:

String str = "[";
for(int i=0; i<arrayList.size(); i++){
  if(i == arrayList.size()-1){
      //Don't add comma at the end.
      str += arrayList.get(i);    
  }
  else{
      str += arrayList.get(i)+",";
  }
}
str += "]";

1 Comment

the output must be in this format [1000000062,1000000095,1000000058]

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.