使用ArrayList的Android GridView适配器

时间:2014-01-07 17:22:25

标签: android json android-gridview

我无法为接受Griview的{​​{1}}创建适配器。我认为Adapter类中的错误行是:ArrayList并且看起来调用viewHldr.wcbc_image_iv.setImageResource(urlStrArrList.get(position));是问题所在。

.setImageResource

此处; 适配器

public class JGrid66 extends Activity {

JSONObject jsonOb;
JSONArray JSArrGallery = null;;
GridView grid65_gv;
JGrid66Adapter2 jGr7Adap;
ProgressDialog mProgressDialog;
ArrayList<String> idStrArrList = new ArrayList<String>();
ArrayList<String> urlStrArrList = new ArrayList<String>();
ArrayList<String> descrStrArrList = new ArrayList<String>();

 // JSON Node names
private static final String TAG_GALLERY = "gallery";
private static final String TAG_GALLERYURL = "galleryurl";
private static final String TAG_ID = "id";
private static final String TAG_GALLERYDESCR = "gallerydescr";

static String FLAG = "flag";

private String jsonUrl = "http://www.mysite.com/apps/wcbc/galleryuil.txt";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.jgrid66);
    grid65_gv = (GridView) findViewById(R.id.jgrid66_gv);   



}//--- END onCreate

//--- DownloadJSON Class
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        JGrid4Adapter jParser = new JGrid4Adapter();
        // getting JSON string from URL
        JSONObject jsonOb = jParser.getJSONFromUrl(jsonUrl);            
        try {
            JSArrGallery = jsonOb.getJSONArray(TAG_GALLERY);
            // looping through All gallery images
            for (int i = 0; i < JSArrGallery.length(); i++) {
                JSONObject galleryJO = JSArrGallery.getJSONObject(i);
                String idStr = galleryJO.getString(TAG_ID);
                String urlStr = galleryJO.getString(TAG_GALLERYURL);
                String descrStr = galleryJO.getString(TAG_GALLERYDESCR);

                idStrArrList.add(idStr);
                urlStrArrList.add(urlStr);
                descrStrArrList.add(descrStr);


            }// -- END for loop
        } catch (JSONException e) {
            e.printStackTrace();
        }// --- END Try
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        jGr7Adap = new JGrid66Adapter2(JGrid66.this, urlStrArrList);
        grid65_gv.setAdapter(jGr7Adap);
        jGr7Adap.notifyDataSetChanged();
    }

}

//--- END DownloadJSON Class
}

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

private ArrayList<String> urlStrArrList;

是字符串的arraylist。如果您有网址,则需要下载图片,然后将其设置为imageview。

setImageResource将资源ID作为参数,这是一个int值。

public void setImageResource (int resId)

Added in API level 1
Sets a drawable as the content of this ImageView.

您可以考虑使用延迟加载Universal Image Loader或使用picasso

Caching images and displaying