0

am trying to pass the array items so that they can be displayed as image views but facing difficulty in implementing it. any help.

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;

class Data.java

 public class Data {

    public int image;
    public String title;
    public String description;

    public Data() {}

}

here is the error am getting if i change data.image = sampleData[0] to 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)
3
  • What difficulty are you facing? A sytax error, an exception? If you can tell, it will be easier for us to help. Commented Jun 19, 2014 at 11:35
  • What are you trying to implement brother? Can you talk us through your idea? Commented Jun 19, 2014 at 11:36
  • that code displays only the same picture in a grid view. i want to display all the pictures in the array differently in a gridview Commented Jun 19, 2014 at 11:39

4 Answers 4

1

Change

data.image = sampleData[0];

to

data.image = sampleData[i];
Sign up to request clarification or add additional context in comments.

2 Comments

forces close when i run it
It is probably because of something else in your code. Share your crash report please.
0

you have a 0 in your brackets. That means you print the same image on every row. Change it to your loop letter which is 'i'

data.image = sampleData[i];

Comments

0

got it working.

Initially i had set SAMPLE_DATA_ITEM_COUNT=10 but when i set it to 4 it worked because it had to match with the number of items in the array. Thanks for the help everyone

1 Comment

replace SAMPLE_DATA_ITEM_COUNT with sampleData.length() in for loop's condition
0

As your sampleData[] has 3 items and SAMPLE_DATA_ITEM_COUNT's value is 10 while you try to access data.image = sampleData[i]; got ArrayIndexOutOfBoundsException when i is greater than 2 so change this

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

to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.