我试图找到一个好的方法来下载图片,并在完成下载后用图片更新用户列表。
因此,我尝试在Android应用中创建用户个人资料列表,在其中显示用户名和旁边的小图片。过去两周我一直在观看教程,慢慢感受到android的所有东西,我的应用程序从我的SQL(通过PHP)服务器中提取数据,没有问题,但我遇到了麻烦找出我可以如何以及在哪里启动异步任务来下载每个用户名的图片。
我正在思考像这样的流程
这种行为有点受到启发,而且#34; Reddit很有趣" app,显示所有文章,并在下载时加载和显示预览图像。在看到图像之前,您会看到一个小旋转圆圈作为占位符。
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;
}
}
}
}
答案 0 :(得分:0)
Okie感谢Nasch,这似乎有助于我朝着正确的方向前进
String username = allUserList.get(0).get(TAG_USERNAME);
Log.d("And the winner is", username);
因此打印出用户名字符串。使用URL来实现这一点可能类似,但我的主要问题是试图找出如何从列表中提取数据并且现在似乎已经解决了:D(最后3天后, phew )
有一种感觉,这是一件我想不通的事情。