2013-11-28 52 views
0

我有一个场景,我在onclicklistener的按钮中调用asynctask。在后期执行的AsyncTask我加入的ArrayList的,我想返回的方法值的方法调用的AsyncTask将arraylist从asynctask传递给调用它的方法android

这是我的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"; 
          } 
         } 

    } 

我想通过TOP1,TOP2,TOP3的ArrayList调用方法 请帮我实现这一点。

+0

你想在哪里返回?在全球范围内声明你的数组列表有什么问题可以正确解释它? –

+0

我想将arraylist返回到调用asynctask的方法,它是onclicklistener按钮。 –

回答

1

您可以使用的ArrayLists的ArrayList:

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

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

return tops; 
+0

我应该如何从postexecute方法返回arraylist? –

+1

返回意味着不是实际的返回,因为onPostExecute不返回任何你可以使用处理程序或最简单的方法你可以调用另一个函数与此param.Do研究更多关于AsyncTask。 – Subbu

0

哎只需拨打的AsyncTask在BTN点击 ,并在后期执行方法 调用,它会使用数组,它是另一种方法,在后期执行中创建。

@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"; 
         } 
        } 
+0

我想在其中调用的AsyncTask –

+0

你不能这样做,因为在BTN点击听者的ArrayList将null,因为的AsyncTask就像线程buttonclicklistener使用此ArrayList。所以你想使用arraylist,那么你应该使用第二种方法在后执行数组列表数据执行操作。 – khurram

+0

第二种方法在后期执行手段? –