[英]looping in primitive array android java
我試圖循環 JSONArray 以將其值放入原始數組中,但結果始終為null ,這是我的代碼(代碼 1 ):
try{
JSONArray data = new JSONArray(str);
/for(int i=0;i<=data.length();i++)
{
JSONObject videos = data.getJSONObject(i);
videoListData = new VideoData[]{new VideoData("aaa",getBitmap(videos.getString("url")))};
}
VideoAdapter adapter = new VideoAdapter(videoListData);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
我試圖把它放在沒有循環和成功的情況下,這是我的代碼(代碼 2 ):
VideoData[] videoListData = new VideoData[]{
new VideoData("Email", getBitmap("xaf234")),
new VideoData("Info", getBitmap("xaf290")),
new VideoData("Info", getBitmap("1f7ucv5"))
Q : 如何在循環中實現代碼2 ?
請幫忙
謝謝。
每次循環時都會創建一個新數組。 那不是你想要做的; 你想在每個循環中添加到數組中。
Java 數組的大小是固定的,您需要事先知道大小。 因此,不要使用它們; 使用 ArrayList 代替。
因此:
List<VideoData> videoListData = new ArrayList<VideoData>();
// the rest of your code, except...
videoListData.add(new VideoData("aaa", getBitmap(...));
在您的 for 循環中,您每次都分配新值,但您需要在 videoListData 中添加數據,例如 videoListData.add(new VideoData("aaa",getBitmap(videos.getString("url"))));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.