sli*_*n77 2 android textview android-imageview android-asynctask
我AsyncTask在另一个AsyncTask(2)中使用(1 ).AsyncTask 1获取在线用户数据,计算响应中的条目数以及每个条目,onPostExecute显示用户名并运行新的AsyncTask(2)以从服务器获取图像并将其加载到ImageView.这一切都发生在onPostExecute.这完美地工作,获取并显示用户数据,并且每个条目逐个显示图像.
然而,通过阵列的itteration和的更新TextView中AsyncTask1的onPostExecute情况发生得这么快,它基本上只显示数组中的最后一个用户名,其他的人被加载,但无法与人眼检测:)
同时,AsyncTask2仍在从网上获取图像,并显示错误用户的个人资料图像.我在这里遇到的问题显然是这两个需要同步.所以我以为我只是等待方法中的输出AsyncTask2 get(),但现在根本没有任何更新,不TextView...这对我来说是意想不到的行为.
那么,问题是如何同步2 AsyncTasks?
如果仍然需要,可以澄清一些代码
//instantiate first AsyncTask
new AsyncRequest().execute(bundle);
private class AsyncRequest extends AsyncTask<Bundle, Void, String> {
protected String doInBackground(Bundle... bundle) {
String data = null;
try {
data = request(null, bundle[0]); //request the data
return data;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}// end method
protected void onPostExecute(String response) {
JSONArray data = null;
try {
JSONObject response2 = Util.parseJson(response);
data = response2.optJSONArray("data");
int amount = data.length();
TextView s1 = (TextView) findViewById(R.id.some_id);
s1.setText("" + amount); //displays number of items
//display the data
for(int i=0; i<amount; i++){
String email = "";
String id = "";
JSONObject json_obj = data.getJSONObject(i);
Log.d("JSONObject ", ""+json_obj);
String name = json_obj.getString("name");
if (json_obj.has("email")){
email = json_obj.getString("email");
}
if (json_obj.has("id")){
id = json_obj.getString("id");
}
String picture = "http://www.domain.com/"+id+"/picture";
TextView s2 = (TextView) findViewById(R.id.name_placeholder);
s2.setText(name);
//here we do a new AsynTask for each entry and wait until the data is fetched
new DownloadProfileImageTask().execute(picture, name).get();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}// end method
Run Code Online (Sandbox Code Playgroud)
为什么要调用具有多个名称setText的单个单词并不十分清楚TextView.正如您所提到的,尽管您拥有setText所有名称,但您只能看到一个名称.也许你需要使用ListView或类似的东西.
现在关于你的问题:可能你不需要两个AsyncTask.你可以做到一切AsyncTask.代码如下所示:
//Create a Holder class as a data holder.
//For simplicity, public attributes are used
class Holder{
public String name;
public String email;
public String id;
public BitmapDrawable imageDrawable;
}
//instantiate the AsyncTask
new AsyncRequest().execute(bundle);
private class AsyncRequest extends AsyncTask<Bundle, Holder, Integer> {
protected Integer doInBackground(Bundle... bundle) {
int amount = 0;
try {
data = request(null, bundle[0]); //request the data
JSONArray data = null;
JSONObject response2 = Util.parseJson(response);
data = response2.optJSONArray("data");
amount = data.length();
//display the data
for(int i=0; i<amount; i++){
Holder holder = new Holder();
holder.email = "";
holder.id = "";
JSONObject json_obj = data.getJSONObject(i);
Log.d("JSONObject ", ""+json_obj);
holder.name = json_obj.getString("name");
if (json_obj.has("email")){
holder.email = json_obj.getString("email");
}
if (json_obj.has("id")){
holder.id = json_obj.getString("id");
}
String picture = "http://www.domain.com/"+id+"/picture";
//Fetch the image and create a Drawable from it - Synchronously
holder.imageDrawable = getImageDrawable(picture, name);
publishProgress(holder);
}
} catch (Exception e) {
e.printStackTrace();
}
return amount;
}// end method
protected void onProgressUpdate(Holder... holder) {
//Update the user name and image
TextView s2 = (TextView) findViewById(R.id.name_placeholder);
s2.setText(holder[0].name);
ImageView imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageDrawable(holder[0].imageDrawable);
}
protected void onPostExecute(Integer amount) {
TextView s1 = (TextView) findViewById(R.id.some_id);
s1.setText(amount.toString()); //displays number of items
}// end method
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5427 次 |
| 最近记录: |