Ankita Deshmukh
Ankita Deshmukh

Reputation: 237

pass arraylist from asynctask to the method calling it android

I am having a scenario in which I am calling asynctask in onclicklistener of button. In post execute method of asynctask I am adding values in arraylist which I want to return to the method calling asynctask

This is my asynctask

 public class FetchtopTask extends AsyncTask<String, String, String>{

            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub

                 try {


                     String getitemcode=params[0];
                        // Building Parameters
                        ArrayList<NameValuePair> params1 = new ArrayList<NameValuePair>();
                       params1.add(new BasicNameValuePair("ItemCode",getitemcode));
                        Log.d("request!", "starting");
                        response = CustomHttpClient.executeHttpPost("http://10.0.2.2/top.php",params1);


                        String retstring=response.toString();
                        Log.d(retstring,"stringggggg");
                        return retstring;
                    }
                 catch(Exception io){
                 msg = "No Network Connection";
                 }
                 return null;
           }


             @Override
                protected void onPostExecute(String sJson) {
                          try {
                                    JSONArray aJson = new JSONArray(sJson);


                                    for(int i=0;i<aJson.length();i++)
                                    {
                                         JSONObject jsonO = aJson.getJSONObject(i);
                                         top1.add(jsonO.getString("MName"));
                                         top2.add(jsonO.getString("Amount"));
                                         top3.add(jsonO.getString("TaxStruct"));

                               }

                          }catch(JSONException e){
                               msg = "Invalid response";
                           }
                        }

     }

I want to pass top1,top2,top3 arraylists to calling method please help me to achieve this.

Upvotes: 0

Views: 787

Answers (2)

khurram
khurram

Reputation: 1362

hey Just call AsyncTask in btn Click and in post execute method call another method which will use that Array which is created in post Execute.

@Override
            protected void onPostExecute(String sJson) {
                      try {
                                JSONArray aJson = new JSONArray(sJson);
                                for(int i=0;i<aJson.length();i++)
                                {
                                     JSONObject jsonO = aJson.getJSONObject(i);
                                     top1.add(jsonO.getString("MName"));
                                     top2.add(jsonO.getString("Amount"));
                                     top3.add(jsonO.getString("TaxStruct"));

                                     anotherMethod(top);
                           }
                      }catch(JSONException e){
                           msg = "Invalid response";
                       }
                    }

Upvotes: 0

Subramanian Ramsundaram
Subramanian Ramsundaram

Reputation: 1347

You may use ArrayList of ArrayLists:

ArrayList<ArrayList<String>> tops= new ArrayList<ArrayList<String>>();

tops.add(top1);
tops.add(top2);
tops.add(top3);

and

return tops;

Upvotes: 1

Related Questions