【发布时间】:2014-07-07 13:55:23
【问题描述】:
我想在 LoadAllProduct 中传递一个变量,将 AsyncTask 扩展为 Fragment1 扩展 Fragment,但我不知道该怎么做。 这是我的部分代码:
public class MainChauffeur extends FragmentActivity implements TabListener{
JSONParser jParser = new JSONParser();
.
.
.
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.activity_main_chauffeur);
LoadAllProduct a=new LoadAllProduct();
a.execute();
}
class LoadAllProduct extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(c);
pDialog.setMessage("Chargement Travail...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Product: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
Intent t=new Intent();
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
id = c.getString(TAG_PID);
this.name = c.getString(TAG_NAME);
/**
** i want to pass this.name in the Fragment1.java **
**/
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/** * 完成后台任务后关闭进度对话框 * **/
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() {
}
});
}
}
}
//这是类片段
public class Fragment1.java extends Fragment {
}
【问题讨论】:
-
我已经回答了你之前的问题,非常相似。 stackoverflow.com/a/24608867/2291104 主要思想是使您的 AsyncTask 静态并在构造函数中传递数据。
-
感谢您的快速答复!我很抱歉我的精神错乱!谢谢你的链接!!
-
我使用了静态类,但变量 this.name 在 MainChauffeur.java 中使用时仍然为空
标签: java android android-asynctask fragment android-fragmentactivity