I have a fragment in side a activity. I want to make a class that extend AsyncTask separately. How i can execute the AsyncTask from fragment and get value of Asyntask class in fragment. for example suppose it is my fragment class
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);}
}
and here is 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();
}
}
I want all value in my fragment that is retrieve in doInBackground
method variable like feecode,feehead,period etc