1

How to get 2 array list (1 String ,1 Integer) from asynctask and print them in Mainactivity??

This my AsyncTask:

private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
    @Override

    protected String doInBackground(String... urls) {

        return readJSONFeed(urls[0]);
    }
    protected void onPostExecute(String result) {
        try {
            JSONArray jsonArray = new JSONArray(result);
            Log.i("JSON", "Number of surveys in feed: " +jsonArray.length());
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Toast.makeText(getBaseContext(), jsonObject.getString("city"),Toast.LENGTH_SHORT).show();

               x.setStation_name(jsonObject.getString("city"));
               x.setStation_petrol_amount(jsonObject.getInt("amount"));

            }

            System.out.println("*******"+x.getStation_name());
            System.out.println("*******"+x.getStation_petrol_amount());



        } catch (Exception e) {
            e.printStackTrace();
        }

    }
        }

Here my output in system.out.print >> and it's OK.

This is my_Data class:

 public      ArrayList<String> city = new ArrayList<>() ;
      public      ArrayList<Integer> amount = new ArrayList<>() ;


    public  void setStation_petrol_amount(int station_petrol_amount) {
        amount.add(station_petrol_amount);
    }

    public  void setStation_name(String station_name)
    {
        city.add(station_name);
    }

       public  ArrayList<String> getStation_name()
    {
        return city;
    }

  public  ArrayList<Integer> getStation_petrol_amount() {
        return amount;
    }

My mainActivity:

    new ReadJSONFeedTask().execute();
    ReadJSONFeedTask ob = new ReadJSONFeedTask();
        ob.execute("http://192.168.12.1/server/index.php");
        System.out.println(">>>>:"+x.getStation_name());
        System.out.println(">>>>:"+x.getStation_petrol_amount());

The output if system.out.print >> has no data, how to get it??

Unheilig
  • 16,196
  • 193
  • 68
  • 98
m4m4
  • 19
  • 4
  • 1
    It would be nice if you rephrase your question. What would you like to achieve? You can keep both integer and string in the same arraylist of type my_Data. – Rethinavel Mar 23 '15 at 09:57
  • but i need the two arraylist in my work – m4m4 Mar 23 '15 at 10:00
  • just i need get data from my_Data but when code run my_Data class still no have data in this time – m4m4 Mar 23 '15 at 10:01
  • It is due to its execution in Background, and have you considering using SparseArray which might suites batter for type. – Apar Amin Mar 23 '15 at 10:03
  • please iam not good enough in android can someone solve my problem by writing the code that i need or explan by example what i should do – m4m4 Mar 23 '15 at 10:28

1 Answers1

0

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

see the answer for the question.. you can pass your 2 array lists as parameter of the callback method to called activity once the thread finishes the work

in that example the interface should be modified like this

public interface AsyncResponse {
void processFinish(List<String> firstlist,List<Integer> secondlist);}

then in post execute you have to pass the result arraylist as parameters

OR you can pass the x objects list to called activity by replacing the interface like below

   public interface AsyncResponse {
void processFinish(List<X> firstlist);}
Community
  • 1
  • 1
Dinesh Kannan
  • 1,255
  • 13
  • 32