我正在尝试传递数组项目,以便它们可以显示为图像视图,但在实现它时遇到困难。任何帮助。
class sampleData.java
public static ArrayList<Data> generateSampleData() {
int sampleData[] = {
R.drawable.imageone,
R.drawable.imagetwo,
R.drawable.imagethree
};
String repeat = " repeat";
final ArrayList<Data> datas = new ArrayList<Data>();
for (int i = 0; i < SAMPLE_DATA_ITEM_COUNT; i++) {
Data data = new Data();
data.image = sampleData[0];
data.title = "Pinterest Card";
data.description = "Super awesome description";
Random ran = new Random();
int x = ran.nextInt(i + SAMPLE_DATA_ITEM_COUNT);
for (int j = 0; j < x; j++)
data.description += repeat;
datas.add(data);
}
return datas;
类Data.java
public class Data {
public int image;
public String title;
public String description;
public Data() {}
}
如果我将data.image = sampleData [0]更改为,则会出现错误 data.image = sampleData [i]
06-19 11:51:02.814 1626-1626/com.richapps.true_gamer E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at com.richapps.true_gamer.grid.SampleData.generateSampleData(SampleData.java:21)
at com.richapps.true_gamer.FindPeopleFragment.onCreateView(FindPeopleFragment.java:29)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
at android.app.BackStackRecord.run(BackStackRecord.java:622)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
更改
data.image = sampleData[0];
到
data.image = sampleData[i];
答案 1 :(得分:0)
括号中有0。这意味着您在每一行上打印相同的图像。将其更改为您的循环信函,即&#39; i&#39;
data.image = sampleData[i];
答案 2 :(得分:0)
让它发挥作用。
最初我设置了SAMPLE_DATA_ITEM_COUNT = 10,但当我将其设置为4时,它工作正常,因为它必须与数组中的项目数相匹配。感谢大家的帮助
答案 3 :(得分:0)
由于sampleData[]
有3个项目且SAMPLE_DATA_ITEM_COUNT's
值为10,而您在data.image = sampleData[i];
时尝试访问i is greater than 2
获得ArrayIndexOutOfBoundsException,因此更改此
for (int i = 0; i < SAMPLE_DATA_ITEM_COUNT; i++) {
Data data = new Data();
data.image = sampleData[0];
data.title = "Pinterest Card";
data.description = "Super awesome description";
Random ran = new Random();
int x = ran.nextInt(i + SAMPLE_DATA_ITEM_COUNT);
for (int j = 0; j < x; j++)
data.description += repeat;
datas.add(data);
}
到
for (int i = 0; i < sampleData.length(); i++){
Data data = new Data();
data.image = sampleData[i];
data.title = "Pinterest Card";
data.description = "Super awesome description";
Random ran = new Random();
int x = ran.nextInt(i + SAMPLE_DATA_ITEM_COUNT);
for (int j = 0; j < x; j++)
data.description += repeat;
datas.add(data);
}