开发者

using json data in my gridview array

I am messi开发者_运维问答ng around with my own gallery app that loads images (thumbs and full size) from a webserver. I have my image info returned from a database and encoded as json data.

{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}

I am using the developer.android.com hello gridview tutorial as my reference. They are using a url referencing local images.

MY QUESTIONS.

  1. Should I parse my json data into separate arrays since I will be using all of the data at some point in my app or should I keep it all together?

  2. how can I replace the static image array found below with my thumbnail images?

Static Image array

private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};


The best way is to definea class that has member variables id,name,thumb,path.

public class foo{
    public int id;
    public String name;
    public String thumb;
    public String path;
}

Then just parse your arrays into an ArrayList of this bject type.

ArrayList<foo> myFooArray = new ArrayList<foo>();

You can call the add method on the myFooArray to append a foo object:

myFooArray.add(new foo());

To answer the second part of your question you would reference the thumb member of the object in your array

mThumbIds[position] becomes myFooArray[position].thumb

of course you will need to load the image from file rather than resource, you should easily be able to find an example of this but for a clue you can use something like setImageDrawable(Drawable.createFromPath(myFooArray[position].thumb);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜