简体   繁体   中英

How to send values from an arraylist from AsyncTask class to another class in Android?

I have the next problem, I tried to send Arraylist<string> values from AsyncTask class to other class call graphics but I don't know what I do wrong and how to get Arraylist<string> values in the other class, because I have a lot of sintaxis errors I my code

AsyncTask class

public class fetch  extends AsyncTask<Void,Void,ArrayList<String>> {
    //v funcional


        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        protected ArrayList<String> doInBackground(Void... voids) {

                    String Id ="";
                    String Time ="";
                    String Pressure="";

                    ArrayList<String> IdArray = new ArrayList<String>();
                    ArrayList<String> TimeArray = new ArrayList<String>();
                    ArrayList<String> PressureArray = new ArrayList<String>();
                    String IdS=""+IdArray;
                    String TimeS=""+TimeArray;
                    String PresureS=""+PressureArray;
                    data.set(1,TimeS);
                    data.set(2,TimeS);
                    data.set(3,TimeS);

            return data;
        }

        @Override
        protected void onPostExecute(ArrayList<String> result){
            super.onPostExecute(result);
            Graphics.data.setText(data);

        }}

The graphics class

 public class Graphics extends AppCompatActivity {
        public static TextView data;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.graphics);

            Button firstButton = (Button) findViewById(R.id.json);
            data = (TextView) findViewById(R.id.msgTxt);
            firstButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    fetch process = new fetch();
                   ArrayList<String> data= process.execute();
                   data.get(1);
                }
            });
    }

you can create an interface which your "Graphics" implements it , and pass it to your AsyncTask class. like this

public interface BlaBlaBla {
    void doBla(ArrayList<String> myBla);
}

and in your "Graphics" :

class Graphics extends AppCompatActivity implements BlaBlaBla {
fetch process = new fetch(this);
}

and for asyncClass :

public class fetch  extends AsyncTask<Void,Void,ArrayList<String>> {
 //constructor
BlaBlaBla bla;
public fetch(BlaBlaBla bla){
this.bla=bla;
}
//when your task is complete use bla.doBla(pass your array here);
}

my solution Fetch class

public class Fetch  extends AsyncTask<Void,Void,String[][]> {
    public static int KEY = 0;
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
     protected String[][] doInBackground(Void... voids) {
          HttpHandler sh = new HttpHandler();
           String url = "http:xxxxxxx/xxx.json";
           String jsonStr = sh.makeServiceCall(url);
           JSONObject jsonObj = null;
            try {
                jsonObj = new JSONObject(jsonStr);
                int nTiempos=jsonObj.length();
                String[] IdKeyArray = new String[nTie];
        for (int i = 0; i < jsonObj.length(); i++) {
                  JSONObject c = jsonObj.getJSONObject(String.valueOf(i));
                  IdKeyArray[i] = c.getString("Key");
                  String[][] valores={IdKeyArray};
                return valores;
            } catch (JSONException e) {
            e.printStackTrace();
        }
       return null;
   }
}

And this is the "call" the other class where I get the values

private String[][] valores;
Fetch process = new Fetch();
  process.execute();
   try {
      valores=process.get();
     } catch (ExecutionException e) {
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      pm.setText(String.valueOf(valores[Fetch.Key][0]));
   }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM