Android 不确定如何从列表的每个元素获取数据,以便从异步任务下载和显示其中的图像
伙计们,再见 我正在努力找到一种下载图片的好方法,并在下载完成后用图片更新用户列表 因此,我试图在android应用程序中创建一个用户配置文件列表,其中显示用户的用户名和旁边的一张小图片。在过去的两周里,我一直在看教程,慢慢地对安卓系统有所了解,我让我的应用程序从我的SQL(通过PHP)服务器上毫无问题地下载数据,但我很难确定如何以及在哪里启动异步任务来下载每个用户名的图片 我在想这样的事情Android 不确定如何从列表的每个元素获取数据,以便从异步任务下载和显示其中的图像,android,image,listview,asynchronous,download,Android,Image,Listview,Asynchronous,Download,伙计们,再见 我正在努力找到一种下载图片的好方法,并在下载完成后用图片更新用户列表 因此,我试图在android应用程序中创建一个用户配置文件列表,其中显示用户的用户名和旁边的一张小图片。在过去的两周里,我一直在看教程,慢慢地对安卓系统有所了解,我让我的应用程序从我的SQL(通过PHP)服务器上毫无问题地下载数据,但我很难确定如何以及在哪里启动异步任务来下载每个用户名的图片 我在想这样的事情 用户单击“刷新”按钮以 应用程序在Web服务器上与PHP对话并下拉用户列表(到目前为止是否正确) 解析
- 用户单击“刷新”按钮以
- 应用程序在Web服务器上与PHP对话并下拉用户列表(到目前为止是否正确)
- 解析从服务器获取的所有数据并将其转换为列表(到目前为止是否正确)
- 列表中的数据包含配置文件图片的URL(是否正确)
- 逐个检查每个列表元素并开始异步任务以下载图片(不知道如何逐个元素从列表中提取URL)
- 下载图片后,将其显示(如上所述,不知道如何更新每个元素)
package com.example.administrator.hellopants;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class UserlistActivity extends Activity{
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> allUserList;
private static String url_all_users = "http://myurl.com/php/db_list_all_users.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_USERLIST = "userlist";
private static final String TAG_ID = "id";
private static final String TAG_USERNAME = "username";
JSONArray userlist = null;
Button buttonRefresh;
@Override
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_userlist);
allUserList = new ArrayList<HashMap<String,String>>();
buttonRefresh = (Button) findViewById(R.id.button_refresh);
buttonRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new PopulateUserList().execute();
}
});
}
class PopulateUserList extends AsyncTask<String, String, String> {
ListView lvItems = (ListView) findViewById(R.id.listview_usernames);
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(UserlistActivity.this);
pDialog.setMessage("Loading users details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_users, "GET", params);
Log.d("All Usernames JSON Output: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
userlist = json.getJSONArray(TAG_USERLIST);
// looping through All Products
for (int i = 0; i < userlist.length(); i++) {
JSONObject c = userlist.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_USERNAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_USERNAME, name);
// adding HashList to ArrayList
allUserList.add(map);
}
} else {
// no products found
}
} 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(
UserlistActivity.this, allUserList,
R.layout.list_userliststyle, new String[] { TAG_ID,
TAG_USERNAME},
new int[] { R.id.list_userliststyle_id, R.id.list_userliststyle_username });
// updating listview
lvItems.setAdapter(adapter);
}
});
}
}
public class DownloadImageBackground extends AsyncTask<String, Void, Drawable> {
ImageView imageView;
String myURL = null;
@Override
protected Drawable doInBackground(String...strings) {
this.myURL = strings[0];
Log.i("doInBackground", "Loading image from: "+ myURL.toString());
//TODO: Pass the correct URL to download_image
return download_Image(myURL);
}
@Override
protected void onPostExecute(Drawable result) {
imageView.setImageDrawable(result);
}
private Drawable download_Image(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, url);
return d;
} catch (Exception e) {
Log.e("download_Image", "Caught exception: "+e.getMessage());
return null;
}
}
}
}
package com.example.administrator.hellopants;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.graphics.drawable.drawable;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.Toast;
导入org.apache.http.NameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.InputStream;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
公共类UserlistActivity扩展活动{
私人对话;
JSONParser jParser=新的JSONParser();
ArrayList冲积层列表;
私有静态字符串url\u所有用户=”http://myurl.com/php/db_list_all_users.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_USERLIST=“USERLIST”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记\u USERNAME=“USERNAME”;
JSONArray userlist=null;
纽扣新鲜;
@凌驾
创建公共void(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity\u用户列表);
allUserList=新的ArrayList();
buttonRefresh=(按钮)findViewById(R.id.Button\u刷新);
buttonRefresh.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
新的PopulateUserList().execute();
}
});
}
类PopulateUserList扩展了AsyncTask{
ListView lvItems=(ListView)findViewById(R.id.ListView_用户名);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(UserlistActivity.this);
setMessage(“正在加载用户详细信息。请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url\u所有用户,“GET”,参数);
Log.d(“所有用户名JSON输出:”,JSON.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
userlist=json.getJSONArray(TAG_userlist);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签标识,标识);
map.put(TAG_用户名、名称);
//将哈希列表添加到ArrayList
添加(地图);
}
}否则{
//没有发现任何产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
runOnUiThread(新的Runnable(){
公开募捐{
ListAdapter=新的SimpleAdapter(
UserlistActivity.this、allUserList、,
R.layout.list_userliststyle,新字符串[]{TAG_ID,
标记_USERNAME},
新int[]{R.id.list\u userliststyle\u id,
String username = allUserList.get(0).get(TAG_USERNAME);
Log.d("And the winner is", username);