2015-05-09 7 views
0
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)); 
} 

當我打印在日誌中DDDDDDD,輸出的URL的ArrayList,但是當我看到GGGGGGG,它改變05-09 [Ljava.lang.String;@b125cf00如何轉換數組列表<string>串[]

+0

嘗試打印:Log.i(「GGGGGGGGGGG」,Arrays.toString(simpleArray)); – WannaBeGeek

回答

1

你可以做到這一點的將ArrayList轉換爲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)); 
} 

,或者您也可以做到這一點的方式 -

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)); 
} 
+0

@Driod我試過兩種方式答案仍然相同 – Naveen

+0

@Naveen檢查與此 - Log.i(「GGGGGGGGGGG」,Arrays.toString(simpleArray)); – NarendraJi

+0

當我從for循環獲取字符串,然後我得到在strBook bt中的網址stroe當im使用gridView.setAdapter(新SampleAdapter(getActivity(),Arraylist 網址)從我的片段,然後它給出null暴力異常 – Naveen

0
ArrayList<String> list = new ArrayList() ; 
String[] array = list.toArray(new String[list.size()]); 

從Java文檔在
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 
+0

我已經使用過很多次了,它一直都是爲我工作的,我曾經看到有人給它一個0大小的數組,但是正如Java文檔所說,如果數組不大,則允許有一個具有正確容量的新數組 – EpicPandaForce

+0

我做了這個bt仍然存在空指針異常 – Naveen

+0

然後你的ArrayList爲null。 – EpicPandaForce

0

你可以試試這個:

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

我在這篇文章中發現了這段代碼。它可能會幫助您click

相關問題