如何在android中显示来自webservice的图像

时间:2014-04-24 12:22:41

标签: android

我们正在开发一个数据来自webservice的项目。我在listview中显示数据。数据将图像作为图像路径。我已显示所有iformation但我无法显示图像。如何显示图像来自webservices。

我的代码是:

package com.example.doctreachapp;



 import java.util.ArrayList;
 import java.util.HashMap;

 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

 import com.example.doctreachapp.JSONParser;
 import com.example.doctreachapp.R;

 import android.app.ListActivity;
 import android.app.ProgressDialog;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.webkit.WebView;
 import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.SimpleAdapter;
 import android.widget.TextView;


 public class GeneralHospitals extends ListActivity {
private Context context;
private static String url = "my Url";
// private static final String ID = "ID";
String TAG_user_detail = "DentalHospitals";
private static final String Img = "Img";
private static final String Location = "Location";
private static final String URL = "URL";
private static final String Name = "Name";
private static final String Distance = "Distance";

String imagestack;
 Bitmap image; 
JSONArray user_detail_jsonarray = null;
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    lv = (ListView) findViewById(android.R.id.list);
    new ProgressTask().execute();
}

private class ProgressTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPostExecute(final Boolean success) {

        setListView();
        // select single ListView item
        // lv = getListView();
    }

    protected Boolean doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();
        // get JSON data from URL
        // JSONObject jobject = new JSONObject(result);
        JSONObject json = jParser.getJSONFromUrl(url);
        Log.d("json", json.toString());
        try {
            user_detail_jsonarray = json.getJSONArray("DentalHospitals");
            Log.d("jsonarray", user_detail_jsonarray.toString());
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        for (int i = 0; i < user_detail_jsonarray.length(); i++) {
            try {
                JSONObject c = user_detail_jsonarray.getJSONObject(i);
                String img = c.getString(Img);
                String name = c.getString(Name);
                String loc = c.getString(Location);
                String url = c.getString(URL);
                String dis = c.getString(Distance);
                HashMap<String, String> map = new HashMap<String, String>();
                // Add child node to HashMap key & value
                map.put(Img, img);
                map.put(Name, name);
                map.put(Location, loc);
                map.put(URL, url);
                map.put(Distance, dis);
                jsonlist.add(map);
                // Log.d("list",jsonlist);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d("JSONList", jsonlist.toString());
        }
        return true;
    }
}

public class CustomListAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, String>> listData;

    private LayoutInflater layoutInflater;

    public CustomListAdapter(Context context,
            ArrayList<HashMap<String, String>> jsonlist) {
        this.listData = jsonlist;
        layoutInflater = LayoutInflater.from(context);
    }

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

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.Name = (TextView) convertView
                    .findViewById(R.id.name);
            holder.Location = (TextView) convertView
                    .findViewById(R.id.loc);
            holder.URL = (TextView) convertView
                    .findViewById(R.id.url);
            holder.Distance = (TextView) convertView
                    .findViewById(R.id.dis);
            holder.Img = (ImageView) convertView
                    .findViewById(R.id.image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
          //System.out.println(listData.get(position).get("Img"));
            System.out.println(listData.get(position).get("Name"));
        holder.Img.setImageURI(listData.get(position).get("Img"));
        holder.Name.setText(listData.get(position).get("Name"));
        holder.Location.setText(listData.get(position).get("Location"));
        holder.URL.setText(listData.get(position).get("URL"));
        holder.Distance.setText(listData.get(position).get("Distance"));
        //WebView image = holder.Img;

        //imagestack = listData.get(position).get("Img");
        //image = BitmapFactory.decodeStream((listData.get(position).get("Img")).openStream());
        Log.d("image",imagestack);

        return convertView;
    }

}

public static class ViewHolder {
    ImageView Img;
    TextView Location;
    TextView Name;
    TextView URL;
    TextView Distance;
}

public void setListView() {
    // TODO Auto-generated method stub

    CustomListAdapter adapter = new CustomListAdapter(this, jsonlist);
    Log.d("ABCD", "" + lv.getVisibility());
    lv.setAdapter(adapter);

}

}




    Please help me.     

1 个答案:

答案 0 :(得分:0)

在您已初始化getView() holder.Img

ImageView中写下以下代码
 new DownloadImageTask(holder.Img)
            .execute(listData.get(position).get("Img"));

以下AsyncTask下载图片并在ImageView

中显示
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
          ImageView bmImage;

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

          @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

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

          }
        }

如果ListView中的项目数量较少,则使用上述代码,建议您使用LazyList