如何在android的listview中放置2个字符串和从数据库中获取的图像
我有一个数据库,其中存储2个文本字符串和一个图像的URL。我的问题是如何从该URL下载图像,并将其与2个字符串一起放入listview项目中。我的代码获取字符串并从链接下载图像,但图像不会显示在listview中如何在android的listview中放置2个字符串和从数据库中获取的图像,android,json,url,listview,imageview,Android,Json,Url,Listview,Imageview,我有一个数据库,其中存储2个文本字符串和一个图像的URL。我的问题是如何从该URL下载图像,并将其与2个字符串一起放入listview项目中。我的代码获取字符串并从链接下载图像,但图像不会显示在listview中 public class NotificariActivity extends ListActivity { // Progress Dialog private ProgressDialog pDialog; // Creating JSON Parser object JSON
public class NotificariActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, Object>> productsList;
// url to get all products list
private static String url_not = "http://gtc.flavdesign.com/get_notificari.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "notificari";
private static final String TAG_PID = "id";
private static final String TAG_TEXT = "text";
private static final String TAG_DATE = "data";
private static final String TAG_LINK = "link";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_oferte);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, Object>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NotificariActivity.this);
pDialog.setMessage("Loading products. 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_not, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String text = c.getString(TAG_TEXT);
String date = c.getString(TAG_DATE);
String link = c.getString(TAG_LINK);
// creating new HashMap
HashMap<String, Object> map = new HashMap<String, Object>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_TEXT, text);
map.put(TAG_DATE, date);
Bitmap bitmap;
try
{
URL url = new URL(link);
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
bitmap = BitmapFactory.decodeStream(input);
}
catch(Exception e)
{
bitmap=Bitmap.createBitmap(null);
}
map.put("IMAGE",bitmap);
// adding HashList to ArrayList
productsList.add(map);
}
}
} 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(
NotificariActivity.this, productsList,
R.layout.list_oferte, new String[] { TAG_PID,
TAG_TEXT, TAG_DATE, "IMAGE"},
new int[] { R.id.pid, R.id.text, R.id.date, R.id.imageView1});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
公共类通知活动扩展了ListActivity{
//进度对话框
私人对话;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList productsList;
//获取所有产品列表的url
私有静态字符串url_not=”http://gtc.flavdesign.com/get_notificari.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“notificari”;
私有静态最终字符串标记_PID=“id”;
私有静态最终字符串标记_TEXT=“TEXT”;
私有静态最终字符串标记_DATE=“data”;
私有静态最终字符串标记_LINK=“LINK”;
//产品JSONArray
JSONArray产品=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.all_of erte);
//ListView的Hashmap
productsList=新的ArrayList();
//在后台线程中加载产品
新建LoadAllProducts().execute();
}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(NotificariActivity.this);
pDialog.setMessage(“正在加载产品。请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_不是“GET”,参数);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
products=json.getJSONArray(TAG_products);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签PID,id);
map.put(标签文本,文本);
地图放置(标记日期、日期);
位图;
尝试
{
URL=新的URL(链接);
HttpGet-httpRequest=null;
httpRequest=newhttpget(url.toURI());
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
HttpEntity=response.getEntity();
BufferedHttpEntity b_entity=新BufferedHttpEntity(实体);
InputStream输入=b_entity.getContent();
位图=BitmapFactory.decodeStream(输入);
}
捕获(例外e)
{
位图=位图.createBitmap(空);
}
map.put(“图像”,位图);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
pDialog.disclose();
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
这个,产品列表,
R.layout.list_of erte,新字符串[]{TAG_PID,
标记文字,标记日期,“图像”},
新的int[]{R.id.pid,R.id.text,R.id.date,R.id.imageView1});
//更新列表视图
setListAdapter(适配器);
}
});
}
}
}
列表项的我的XML文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<!-- Name Label -->
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:visibility="gone" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="17dip"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="17dip"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/gallery_thumb" />
</LinearLayout>
我无法将其与SimpleAdapter一起使用。文件上说 ImageView。预期的绑定值是资源id或字符串,并调用setViewImage(ImageView,int)或setViewImage(ImageView,string) 但在建造商的文件中: 应在“from”参数中显示列的视图。这些都应该是文本视图 但是,通过扩展BaseAdapter使其正常工作并不太困难,它为您提供了一些灵活性: 我将其添加到NotificanActivity类中:
public class ProductAdapter extends BaseAdapter {
// Hashmap for ListView
ArrayList<HashMap<String, Object>> productsList = new ArrayList<HashMap<String, Object>>();
@Override public int getCount() { return productsList.size(); }
@Override public Object getItem(int position) { return position; }
@Override public long getItemId(int position) { return (Long) productsList.get(position).get(TAG_PID); }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_oferte, null);
holder = new ViewHolder();
holder.pid = (TextView)convertView.findViewById(R.id.pid);
holder.text = (TextView)convertView.findViewById(R.id.text);
holder.date = (TextView)convertView.findViewById(R.id.date);
holder.img = (ImageView)convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.pid.setText((String)productsList.get(position).get(TAG_PID));
holder.text.setText((String)productsList.get(position).get(TAG_TEXT));
holder.date.setText((String)productsList.get(position).get(TAG_DATE));
holder.img.setImageBitmap((Bitmap)productsList.get(position).get("IMAGE"));
return convertView;
}
private class ViewHolder {
public TextView pid, text, date;
public ImageView img;
}
}
private LayoutInflater mInflater;
ProductAdapter pa = new ProductAdapter();
然后productsList.add(m
mInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);