0

I was trying to convert List<String> to String[] allAim= new String[25]; i.e. into allAim string array,
code snippet is:

List<String> myString = new ArrayList<String>();
myString=db.getAllAlarmAim();
Object[] mStringArray = myString.toArray();
for(int i = 0; i < mStringArray.length ; i++) {
    Log.d("string is",(String)mStringArray[i]);
    allAim[i]=(String)mStringArray[i].toString(); 
}

but when I try to access allAim, the app crashes

1

1 Answer 1

2

Based on your code, you can convert List<String> myString to String[] mStringArray as follows:

 Object[] mStringArray = myString.toArray(new String[myString.size()]);

your code will be:

    List<String> myString = new ArrayList<String>();
    myString=db.getAllAlarmAim();
   Object[] mStringArray= myString.toArray(new String[myString.size()]);

    for(int i = 0; i < mStringArray.length ; i++){
        Log.d("string is", (String)mStringArray[i]);
    }       
Sign up to request clarification or add additional context in comments.

5 Comments

you don't need both. either myString.toArray(mStringArray); or mStringArray= myString.toArray(new String[0]); is sufficient
I know, just to be according with their code, i have updated my answer, thank you.
First two lines can be collapsed into List<String> myString = db.getAllAlarmAim();
'for(int i = 0; i < mStringArray.length ; i++){ Log.d("string is", (String)mStringArray[i]); allAlarmAim[i]= (String)mStringArray[i]; } aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,allAlarmAim); l1.setAdapter(aa);' HERE allAlarmAim does not get any values,WHY???
in the code snippet i have mentioned about array allAim, WHY CANT I USE THIS ARRAY OUTSIDE LOOP TO populate list

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.