如何在微调器中检索JSON数据? (JSON数据包括图像URL和文本)

时间:2014-06-28 04:38:01

标签: android json

如果使用Beans,CustomAdapter,ArrayList,如果json数据包含图像URL和文本

,则如何在微调器中检索JSON数据

JSON数据:

{
    "categoryId": 1,
    "categoryName": "Theater",
    "imagePath": "http://dl.dropbox.com/u/40036711/Images/twitter-icon-32.png"
}

我正在使用以下主要类和customAdapter类来获取json:

这是我的CustomAdapter类:

public class CustomAdapter extends ArrayAdapter<Beans> {
ArrayList<Beans> MyList;

LayoutInflater vi;

int Resource;

ViewHolder holder;
    //custom adapter and passing params
public CustomAdapter(Context context, int resource, ArrayList<Beans> objects, Resources res) {
    super(context, resource, objects);
    //Using layoutInflater Service
    vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //declaration of Variables
            Resource = resource;
    MyList = objects;
}

    //getview class for set the position    
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.tvName = (TextView) v.findViewById(R.id.tvName);
        holder.tvId = (TextView) v.findViewById(R.id.tvId);
        holder.ivImage = (ImageView) v.findViewById(R.id.ivImage);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    //holder.imageview.setImageResource(R.drawable.ic_launcher);
    new DownloadImageTask(holder.ivImage).execute(MyList.get(position).getImagePath());
    return v;

}
      //Horlder for declaration
static class ViewHolder {

    public TextView tvName;
    public TextView tvId;
    public ImageView ivImage ;

}
       //Converting  image url into image 
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }


    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }

}
}

这是我的主要活动代码:

try {

       // Getting Array of Contacts
       JSONArray jsonArray = json.getJSONArray("com");

       for (int i = 0; i < jsonArray.length(); i++) {

       JSONObject object = jsonArray.getJSONObject(i);

       //call  Beans class  
       Beans myBeans = new Beans();

               myBeans.setCategoryName(object.getString("categoryName"));

               myBeans.setCategoryId(object.getString("categoryId"));

               myBeans.setImagePath(object.getString("imagePath"));

               //Storing data items in Array list           
       MyList.add(myBeans);
       }

        } catch (ParseException e1) {

           e1.printStackTrace();

        } catch (JSONException e) {
       e.printStackTrace();
    }

    Resources res = getResources(); 

        adapter = new CustomAdapter(FetchImageJson.this,   
                     R.layout.activity_customlayout, MyList,res);

         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个..

JsonObject obj = new JdonObject("pass string here");
String id = obj.getString("categoryId");
String name = obj.getString("categoryName");
String path = obj.getString("imagePath");

设置图片视图

Override
public View getView(int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.tvName = (TextView) v.findViewById(R.id.tvName);
        holder.tvId = (TextView) v.findViewById(R.id.tvId);
        holder.ivImage = (ImageView) v.findViewById(R.id.ivImage);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
      // pass image path url
     URL newurl = new URL(mlist.get(pos)); 
       mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
       holder.imageview.setImageBitmap(mIcon_val);
    return v;

}