不确定如何从列表的每个元素获取数据以从异步任务下载和显示其中的图像
Not sure how to get data from each element of a list to download and display images in it from async task
大家好!
我正在尝试找到一种下载图片并在图片下载完成后用图片更新用户列表的好方法。
所以我正在尝试在 android 应用程序中创建用户配置文件列表,其中显示用户名和旁边的小图片。在过去的 2 周里,我一直在观看教程,慢慢地对所有东西有了感觉 android,并且我让我的应用程序从我的 SQL(通过 PHP)服务器上拉取数据而无需问题,但我无法确定如何以及在何处启动异步任务来为每个用户名下载图片。
我正在考虑像这样的流程
- 用户点击刷新按钮
- 应用程序与网络服务器上的 PHP 对话并拉下用户列表(到目前为止正确执行此操作)
- 解析它从服务器获得的所有数据并将其转换为列表(到目前为止正确执行)
- 列表中的数据包含 URL 个人资料照片(是否正确)
- 一个一个地遍历每个列表元素并开始异步任务下载图片(不知道如何逐个元素地从列表中提取URL )
- 图片下载后显示(如上,不知道如何更新每个元素)
该行为有点受 "Reddit is Fun" 应用程序的启发,因此会显示所有文章并在下载时加载和显示预览图像。在看到图像之前,您会看到一个旋转的小圆圈作为占位符。
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;
}
}
}
}
Okie 感谢 Nasch,这似乎帮助我朝着正确的方向前进
String username = allUserList.get(0).get(TAG_USERNAME);
Log.d("And the winner is", username);
这样就打印出了用户名字符串。用 URL 做这件事可能是相似的,但我的主要问题是试图弄清楚如何从列表中提取数据,现在似乎已经解决了:D(终于在 3 天后,呸)
我觉得这会很简单,但我想不通。
大家好!
我正在尝试找到一种下载图片并在图片下载完成后用图片更新用户列表的好方法。
所以我正在尝试在 android 应用程序中创建用户配置文件列表,其中显示用户名和旁边的小图片。在过去的 2 周里,我一直在观看教程,慢慢地对所有东西有了感觉 android,并且我让我的应用程序从我的 SQL(通过 PHP)服务器上拉取数据而无需问题,但我无法确定如何以及在何处启动异步任务来为每个用户名下载图片。
我正在考虑像这样的流程
- 用户点击刷新按钮
- 应用程序与网络服务器上的 PHP 对话并拉下用户列表(到目前为止正确执行此操作)
- 解析它从服务器获得的所有数据并将其转换为列表(到目前为止正确执行)
- 列表中的数据包含 URL 个人资料照片(是否正确)
- 一个一个地遍历每个列表元素并开始异步任务下载图片(不知道如何逐个元素地从列表中提取URL )
- 图片下载后显示(如上,不知道如何更新每个元素)
该行为有点受 "Reddit is Fun" 应用程序的启发,因此会显示所有文章并在下载时加载和显示预览图像。在看到图像之前,您会看到一个旋转的小圆圈作为占位符。
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;
}
}
}
}
Okie 感谢 Nasch,这似乎帮助我朝着正确的方向前进
String username = allUserList.get(0).get(TAG_USERNAME);
Log.d("And the winner is", username);
这样就打印出了用户名字符串。用 URL 做这件事可能是相似的,但我的主要问题是试图弄清楚如何从列表中提取数据,现在似乎已经解决了:D(终于在 3 天后,呸)
我觉得这会很简单,但我想不通。