3
public SampleGridViewAdapter(Context context,ArrayList<String> urls) {
    this.context = context;
    this .urls=urls;
    Log.i("DDDDDDDDDD",String.valueOf(urls));
    simpleArray = urls.toArray(new String[urls.size()]);
    Log.i("GGGGGGGGGGG",String.valueOf(simpleArray));
}

When I print DDDDDDD in the log, the output is an arraylist of URL's, but when I see GGGGGGG, it changes to 05-09 [Ljava.lang.String;@b125cf00

Nic
  • 12,220
  • 20
  • 77
  • 105
Naveen
  • 43
  • 9

3 Answers3

2

You can do this for converting ArrayList to String[].

public SampleGridViewAdapter(Context context,ArrayList<String> urls) {
    this.context = context;
    this .urls=urls;
    Log.i("DDDDDDDDDD",String.valueOf(urls));
    String[] simpleArray = new String[urls.size()];
    simpleArray = urls.toArray(simpleArray);
    Log.i("GGGGGGGGGGG",String.valueOf(simpleArray));
}

OR you can also do this way -

public SampleGridViewAdapter(Context context,ArrayList<String> urls) {
    this.context = context;
    this .urls=urls;
     Log.i("DDDDDDDDDD",String.valueOf(urls));
     Object[] simpleArray = urls.toArray();

     for(int i = 0; i < simpleArray.length ; i++){
     Log.d("string is",(String)simpleArray[i]);
    }
  Log.i("GGGGGGGGGGG",String.valueOf(simpleArray));
}   
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
  • @Driod i tried the both way bt the answer still same – Naveen May 09 '15 at 07:11
  • @Naveen check with this - Log.i("GGGGGGGGGGG",Arrays.toString(simpleArray )); – Narendra Singh May 09 '15 at 07:21
  • when i m getting string from for loop then i get the urls stroe in strBook bt when i m using gridView.setAdapter(new SampleAdapter(getActivity(),Arraylist urls) from my fragment then it give null ponter exception – Naveen May 09 '15 at 07:25
  • Okay, can you please check exactly what exactly is null? Are you sure the string[] is null? – Narendra Singh May 09 '15 at 07:29
  • Debug and check what logs are you getting...also put try-catch block in constructor of your adapter, so that, you may know the exact issue. – Narendra Singh May 09 '15 at 07:31
0
ArrayList<String> list = new ArrayList() ;
String[] array = list.toArray(new String[list.size()]);

From java docs at
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#toArray(T[])

toArray
public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Specified by:
     toArray in interface Collection<E>
Specified by:
    toArray in interface List<E>
Overrides:
    toArray in class AbstractCollection<E>
Parameters:
    a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
     an array containing the elements of the list
Throws:
     ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
    NullPointerException - if the specified array is null
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

You can try this:

List<String> list = new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        String joined = TextUtils.join(", ", list);
        Log.d("tanim", joined); 

I found this code from this post . it may help you click

Community
  • 1
  • 1
Tanim reja
  • 2,120
  • 1
  • 16
  • 23