0

I'm stuck creating an Adapter for my Griview that accepts an ArrayList. I think the bad line in the Adapter class is: viewHldr.wcbc_image_iv.setImageResource(urlStrArrList.get(position)); and it appears that the call .setImageResource is the problem.

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
}

Here;s the Adapter:

public class JGrid66Adapter2 extends BaseAdapter {  
private ArrayList<String> urlStrArrList;
Context context;

public JGrid66Adapter2(Context context,ArrayList<String> urlStrArrList) {
    super();
    this.urlStrArrList = urlStrArrList;
    }


@Override
public int getCount() {
    return urlStrArrList.size();
}

@Override
    public String getItem(int position) {
    return urlStrArrList.get(position);
    }

@Override
public long getItemId(int position) {
    return 0;
}

    public static class ViewHolder
    {
    public ImageView wcbc_image_iv;
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHldr;
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    if(convertView==null){
        viewHldr = new ViewHolder();
        convertView = inflater.inflate(R.layout.jgrid66_item, null);
        viewHldr.wcbc_image_iv = (ImageView) convertView.findViewById   (R.id.jgrid66_iv);
        convertView.setTag(viewHldr);
    }
    else
    {
        viewHldr = (ViewHolder) convertView.getTag();
    }
    //--- I commented this out because this is where it breaks.
    //viewHldr.wcbc_image_iv.setImageResource(urlStrArrList.get(position));
    return convertView;
}

}

Any help would be great!

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
kirktoon1882
  • 1,221
  • 5
  • 24
  • 40

1 Answers1

0
private ArrayList<String> urlStrArrList;

is arraylist of strings. If you have the url you need to download the images and then set it to imageview.

setImageResource takes a resource id as a param which is an int value.

public void setImageResource (int resId)

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

You may consider using Lazy Loading Universal Image Loader or using picasso

Caching images and displaying

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thank you Raghunandan. What I'm really trying to do is modify Universal Image Loader so that it accepts my ArrayList, so I think you've put me on the correct path. I'm trying to get the above activity to work and then apply it to Universal Image Loader. – kirktoon1882 Jan 07 '14 at 17:51
  • @kirktoon1882 so what is that is not working? Activity has nothing to do with your error. Pls check the implementation of UIL in the link posted in my post. – Raghunandan Jan 07 '14 at 17:58