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

1
  • try printing : Log.i("GGGGGGGGGGG",Arrays.toString( simpleArray )); Commented May 9, 2015 at 6:46

3 Answers 3

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));
}   
Sign up to request clarification or add additional context in comments.

5 Comments

@Driod i tried the both way bt the answer still same
@Naveen check with this - Log.i("GGGGGGGGGGG",Arrays.toString(simpleArray ));
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<string> urls) from my fragment then it give null ponter exception
Okay, can you please check exactly what exactly is null? Are you sure the string[] is null?
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.
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

3 Comments

I have used this many times and it has always worked for me. I have seen people give it a 0-sized array though because as the Java docs say, a new array with the right capacity is allowed if the array is not large enough.
i did this bt still there is null pointer exception
Then your ArrayList is null.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.