albert
albert

Reputation: 251

How to store id in individual string

I am trying to get id as per checkbox selection,using model class,but i want to store single id in single string,user select checkbox and then click on button,on buttons click i user will get ids in string.but its not working following is my code can any one help me with that?

 @Override
        public void onClick(View v) {
            String data = "";

            tet=new ArrayList<String>();

            for (int i = 0; i < aList.size(); i++) {
                 singleStudent = aList.get(i);
                if (singleStudent.isselected() == true) {
                    data = data + "\n" + singleStudent.getPOOJA_LISTING_ID().toString();


                    tet.add(singleStudent.getPOOJA_LISTING_ID());

                    System.out.println("firstid" + tet);
                }
            }

            try {
                for(int j=0; j<tet.size(); j++)
                {
                    Toast.makeText(PoojaSelection.this,
                            "Selected Pooja: \n" + tet.get(j), Toast.LENGTH_LONG)
                            .show();
                    String oneid=tet.get(0);
                    String secid=tet.get(1);
                    String thirdid=tet.get(2);
                    String fourthid=tet.get(3);
                    String fifthid=tet.get(4);
                    System.out.println("tetaray" + oneid+secid+thirdid+fourthid+fifthid);
                }
            }
            catch (Exception e)
            {

            }



        }

Upvotes: 0

Views: 205

Answers (2)

Sourabh Bans
Sourabh Bans

Reputation: 3134

change it as... you are accessing the list on the basis of your index(like.. 1,2,3) not on list's loop index (like get(j))..

String oneid="",secid="", thirdid="" ,fourthid="", fifthid="";

for(int j=0; j<tet.size(); j++)
{
    Toast.makeText(PoojaSelection.this,
            "Selected Pooja: \n" + tet.get(j), Toast.LENGTH_LONG)
            .show();
    if(0==j){
        oneid=tet.get(j);
    }else if(1==j){
        secid=tet.get(j);
    }else if(2==j){
        thirdid=tet.get(j);
    }else if(3==j){
        fourthid=tet.get(j);
    }else if(4==j){
        fifthid=tet.get(j);
    } 
}

Log.e("All strings ", ""+ oneid+" , "+secid+" , "+thirdid+" , "+fourthid+" , "+fifthid);

Upvotes: 1

gatteo
gatteo

Reputation: 1382

The IndexOutOfBoundsException s given because you are trying to retrieve an item with tet.get(index) without knowing if tet.size() is as big as index.

This meaning for example trying tet.get(4); while tet.size()is equal to 3. This can solve your problem:

String ids = "";
String secondId;
for(int j=0; j<tet.size(); j++) {
                    Toast.makeText(PoojaSelection.this,
                            "Selected Pooja: \n" + tet.get(j), Toast.LENGTH_LONG)
                            .show();
                    id=id+tet.get(j);

                     //only log the id string after looping all tet
                    if(j==tet.size()-1){
                       Log.d("id","tetaray: " + id +" ");
                    }
                    // edit: save second id
                    if(j==2){
                       Log.d("Second","Second id " + j );
                       secondId=""+j;
                    }
                }

Upvotes: 1

Related Questions