0

I have multiple strings like st1, st2, st3, st4, st5, st6, st7. now i want to get that strings dynamically through for loop using its counter(in my case int i). like "st"+ i , but the android doesnot accept it so what should i do to get the string dynamically. for more info here is the chunk of the code

String image1 = cData.getString(cData.getColumnIndex("fld_image_url1"));
String image2 = cData.getString(cData.getColumnIndex("fld_image_url2"));
String image3 = cData.getString(cData.getColumnIndex("fld_image_url3"));
String image4 = cData.getString(cData.getColumnIndex("fld_image_url4"));
ArrayList<String> imagesArray = new ArrayList<String>();

 //for adding the string in the arraylist dynamically
for(int i=1;i<=4;i++){
    if("image"+i!=null){
        imagesArray.add(String.valueOf("image"+ i));    
    }
}

2 Answers 2

3

How about something like:

List<String> imagesArray = new ArrayList<String>();
for (int i = 1; i <= 4; i++) {
    String image = cData.getString(cData.getColumnIndex("fld_image_url" + i));
    if (image != null) {
        imagesArray.add(image);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

if you want to get value from cursor dynamically then change your code as:

 //for adding the string in the arraylist dynamically
for(int i=1;i<=4;i++){

        int count = cData.getColumnIndex("fld_image_url"+i));
        if(count != -1)
         imagesArray.add(cData.getString(count)); 
}

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.