我在活动中有一个片段。我想创建一个分别扩展AsyncTask的类。我如何从片段中执行AsyncTask并获取片段中Asyntask类的值。 例如,假设它是我的片段类
public class FeePayFragment extends android.support.v4.app.Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_fee_pay, container, false);}
}
这里是Asyntask
public class AsyFeeDues extends AsyncTask<Void,Void,Void> {
JSONArray rootJsonArray;
JSONObject rootJsonObject;
private String compCode,session,admNo;
boolean checkSeverResponse;
public AsyFeeDues(String compCode, String session, String admNo) {
this.compCode = compCode;
this.session = session;
this.admNo = admNo;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Please Wait....");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
RestApi api = new RestApi();
try {
rootJsonObject = api.GetFeeDues(compCode, session, admNo);
checkSeverResponse = (rootJsonObject != null);
if(checkSeverResponse) {
boolean isSuccess = rootJsonObject.optBoolean(TAG_SUCESSFULL);
rootJsonArray = rootJsonObject.optJSONArray(TAG_VALUE);
for (int i = 0; i < rootJsonArray.length(); i++) {
JSONObject jsonObject = rootJsonArray.optJSONObject(i);
int amount = jsonObject.optInt(TAG_AMOUNT);
if (amount > 0) {
int period = jsonObject.optInt(TAG_PERIOD);
if (period <= periodMonth) {
String feeCode = jsonObject.optString(TAG_FEECODE);
String feeHead = jsonObject.optString(TAG_FEEHEAD);
String periodName = jsonObject.optString(TAG_PERIODNAME);
boolean checkFeeKey = feeDuesDetails.containsKey(feeHead);
if (checkFeeKey) {
totalBalanceAmount = totalBalanceAmount + amount;
Map p = feeDuesDetails.get(feeHead);
int previousAmount = (Integer) p.get(TAG_AMOUNT);
int upDateAmount = previousAmount + amount;
feeDuesDetails.get(feeHead).put(TAG_AMOUNT, upDateAmount);
} else {
totalBalanceAmount = totalBalanceAmount + amount;
map = new HashMap();
map.put(TAG_AMOUNT, amount);
feeDuesDetails.put(feeHead, map);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
if(checkSeverResponse) {
if (feeDuesDetails.size() > 1) {
Set keys = feeDuesDetails.keySet();
for (Iterator i = keys.iterator(); i.hasNext(); ) {
String key = (String) i.next();
feeHead.add(key);
Map map = feeDuesDetails.get(key);
int feeHeadWiseAmount = (Integer) map.get(TAG_AMOUNT);
feeAmount.add(feeHeadWiseAmount);
}
feeDuesAdapter = new FeeDuesAdapter(getActivity(), feeHead, feeAmount);
feeDuesList.setAdapter(feeDuesAdapter);
totalAmountInRs.setText(String.valueOf(totalBalanceAmount));
paybleAmount.setText("Total Payable Amount");
}
else {
Toast.makeText(getActivity(), "Your fee paid up to this month", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(getActivity(), "Server not response", Toast.LENGTH_LONG).show();
}
}
我希望我的片段中的所有值都在doInBackground
方法变量中检索,如费用代码,费用,期间等
答案 0 :(得分:0)
你是什么意思?要从Asynctask中获取对象,必须在do outputHTML += ytPlayer.prop('outerHTML');
中返回一个值。为了做到这一点,你必须在对象(模型)中解析你的JSON,这样你就可以返回一个费用对象。完成此操作后,您必须在加载数据时更新您的ui。
答案 1 :(得分:0)
AsyncTask有三个为它定义的参数:
异步任务使用的三种类型如下:
- Params,执行时发送给任务的参数类型。
- 进展,期间发布的进度单位的类型 背景计算。
- 结果,结果的类型 背景计算。
醇>
浏览google文档以获得澄清: http://developer.android.com/reference/android/os/AsyncTask.html
您可以通过执行以下操作来实现此目的:
处理此类事情的界面:
public interface MyListener{
public void onTaskComplete(int status, String data);
}
然后你的asynctask
public class MyAsyncTask extends AsyncTask<Void, Void, String>{
MyListener mMyListener;
public MyAsyncTask(MyListener myListener){
mMyListener = myListener;
}
...
public String doInBackground(Void... params){
String valueToReturn;
//Your code here to get the required data to set the "valueToRetun"
return valueToReturn;
}
...
public void onPostExecute(String data){ <- you get that data here
//Your code here
mMyListener.onTaskComplete(1, data); <- 1 being the success status
}
}
让您的活动实现监听器:
public class MyActivity implements MyLisntener{
...Your code here
public void startTask(){
MyAsyncTask mt = new MyAsyncTask(this);
mt.execute();
}
@Override
public void onTaskComplete(String data){
//Process your data here
}
...Your code here
}
我假设您需要返回String。如果您希望asynctask返回其他内容,请将其替换为您的模态对象。
希望有所帮助:)