android - 如何将图像从url加载到listview中

时间:2013-06-22 09:27:45

标签: android json image url listview

我需要将一些来自url的图像加载到我的ListView中。我想加载的图片网址来自我的localhost中的PHP文件。 我的代码受到以下链接的启发: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

这是我的代码:

public class HealthFaculty extends ListActivity {
private ProgressDialog pDialog;

JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> healthFacultyNameList;

private static String url_hf_name = "http://10.0.2.2/get_hf.php";

private static final String tagSuccess = "success";
private static final String tagHealthFacultyList = "hf_list";
private static final String tagHealthFacultyId = "health_faculty_id";
private static final String tagHealthFacultyName = "health_faculty_name";
private static final String tagHealthFacultyLat = "longitude";
private static final String tagHealthFacultyLong = "latitude";
private static final String tagHealthFacultyPhoto = "photo";

JSONArray healthFacultyName = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_hf);

    healthFacultyNameList = new ArrayList<HashMap<String, String>>();

    new LoadHealthFacultyName().execute();
}

class LoadHealthFacultyName extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(HealthFaculty.this);
        pDialog.setMessage("Loading health faculties name. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_hf_name, "GET", params);

        Log.d("All Health Faculty: ", json.toString());

        try {
            int success = json.getInt(tagSuccess);

            if (success == 1) {
                healthFacultyName = json.getJSONArray(tagHealthFacultyList);

                for (int i = 0; i < healthFacultyName.length(); i++) {
                    JSONObject c = healthFacultyName.getJSONObject(i);

                    String hfId = c.getString(tagHealthFacultyId);
                    String hfName = c.getString(tagHealthFacultyName);
                    String hfLong = c.getString(tagHealthFacultyLong);
                    String hfLat = c.getString(tagHealthFacultyLat);
                    String hfPhoto = c.getString(tagHealthFacultyPhoto);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(tagHealthFacultyId, hfId);
                    map.put(tagHealthFacultyName, hfName);
                    map.put(tagHealthFacultyLong, hfLong);
                    map.put(tagHealthFacultyLat, hfLat);
                    map.put(tagHealthFacultyPhoto, hfPhoto);

                    healthFacultyNameList.add(map);                     
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                ListAdapter adapter = new SimpleAdapter(HealthFaculty.this, healthFacultyNameList,
                        R.layout.list_health_faculty, 
                        new String[] {tagHealthFacultyId, tagHealthFacultyName, tagHealthFacultyLong, tagHealthFacultyLat, tagHealthFacultyPhoto}, 
                        new int[] { R.id.id, R.id.mainHealthFaculty, R.id.hfLong, R.id.hfLat, R.id.hfPhoto });
                setListAdapter(adapter);
            }
        });

    }

}
}

这是xml ListView:

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_health_faculty" >
</ListView>

这是布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_health_faculty"
android:orientation="horizontal"
android:padding="5dip" >

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:background="@drawable/bg_health_faculty"
    android:padding="3dip" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="88dp"
        android:layout_height="88dp"
        android:layout_weight="563.48"
        android:src="@drawable/logo_hospital" />

</LinearLayout>

<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="93dp"
    android:textColorLink="@android:color/white"
    android:visibility="gone" />

<TextView
    android:id="@+id/hfLong"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="93dp"
    android:textColorLink="@android:color/white"
    android:visibility="gone" />

<TextView
    android:id="@+id/hfLat"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="93dp"
    android:textColorLink="@android:color/white"
    android:visibility="gone" />

<TextView
    android:id="@+id/hfPhoto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="93dp"
    android:textColorLink="@android:color/white"
    android:visibility="gone" />

<TextView
    android:id="@+id/mainHealthFaculty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/thumbnail"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/thumbnail"
    android:gravity="left|center_vertical"
    android:minHeight="93dp"
    android:paddingLeft="6dip"
    android:paddingTop="0dp"
    android:textColor="#ffffff"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="monospace" />

 </RelativeLayout>

请帮助,谢谢:)

0 个答案:

没有答案