我使用JSON从数据库导入数据。我也从数据库中获取数据。我在数组中获取数据。这里我将变量img中的数据作为url获取,现在我想在Listview中显示此路径url。如何做到这一点。请告诉我??
package com.abc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class FoodView extends ListActivity
{
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_products = "http://ambrogroup.com/app/index/file/food";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_IMAGE = "img";
private static final String TAG_DESTINATION = "destination";
private static final String TAG_PRICE = "price";
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.food_view);
productsList = new ArrayList<HashMap<String, String>>(); // Hashmap for ListView
new LoadAllProducts().execute(); // Loading products in Background Thread by explicit function
ListView listview = getListView(); // Get listview
listview.setOnItemClickListener(new OnItemClickListener() { // on seleting single product // launching Edit Product Screen
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
String pname = ((TextView) view.findViewById(R.id.name)).getText().toString();
String pimg = ((TextView) view.findViewById(R.id.img)).getText().toString();
String pdestination = ((TextView) view.findViewById(R.id.destination)).getText().toString();
String pprice = ((TextView) view.findViewById(R.id.price)).getText().toString();
}
});
}
class LoadAllProducts extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(FoodView.this);
pDialog.setMessage("Loading list. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("Getting all the list : ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++)
{
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String img = c.getString(TAG_IMAGE);
String destination = c.getString(TAG_DESTINATION);
String price = c.getString(TAG_PRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_IMAGE, img);
map.put(TAG_DESTINATION, destination);
map.put(TAG_PRICE, price);
productsList.add(map);
}
}
else
{
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
FoodView.this, productsList,R.layout.list_view, new String[] {TAG_PID,TAG_NAME,TAG_IMAGE,TAG_DESTINATION,TAG_PRICE},new int[]
{
R.id.pid, R.id.name,R.id.img,R.id.destination, R.id.price
});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
答案 0 :(得分:1)
使用此库 https://github.com/nostra13/Android-Universal-Image-Loader
它为您提供了在任何地方通过URL显示图像的简便方法,包括ListView。只需使用:
imageLoader.displayImage(imageUri, imageView);
答案 1 :(得分:0)
您需要专注于许多概念,如内存泄漏,高效加载,使用缩放和JSON数据检索调整位图大小。请仔细阅读此链接,然后您就会明白。