猿问

如何将值从 AsyncTask 类的数组列表发送到 Android 中的另一个类?

我有下一个问题,我尝试将Arraylist<string>值从 AsyncTask 类发送到其他类调用图形,但我不知道我做错了什么以及如何Arraylist<string>在另一个类中获取值,因为我的代码有很多 sintaxis 错误


异步任务类


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);


        }}

图形类


 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);

                }

            });

    }


慕娘9325324
浏览 108回答 2
2回答

慕的地10843

您可以创建一个由您的“图形”实现的接口,并将其传递给您的AsyncTask班级。像这样public interface BlaBlaBla {&nbsp; &nbsp; void doBla(ArrayList<String> myBla);}并在您的“图形”中:class Graphics extends AppCompatActivity implements BlaBlaBla {fetch process = new fetch(this);}对于 asyncClass :public class fetch&nbsp; extends AsyncTask<Void,Void,ArrayList<String>> {&nbsp;//constructorBlaBlaBla bla;public fetch(BlaBlaBla bla){this.bla=bla;}//when your task is complete use bla.doBla(pass your array here);}

蛊毒传说

我的解决方案 Fetch 类public class Fetch&nbsp; extends AsyncTask<Void,Void,String[][]> {&nbsp; &nbsp; public static int KEY = 0;&nbsp; &nbsp; @RequiresApi(api = Build.VERSION_CODES.KITKAT)&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp;protected String[][] doInBackground(Void... voids) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpHandler sh = new HttpHandler();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String url = "http:xxxxxxx/xxx.json";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String jsonStr = sh.makeServiceCall(url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject jsonObj = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObj = new JSONObject(jsonStr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nTiempos=jsonObj.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] IdKeyArray = new String[nTie];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonObj.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject c = jsonObj.getJSONObject(String.valueOf(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IdKeyArray[i] = c.getString("Key");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[][] valores={IdKeyArray};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return valores;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;return null;&nbsp; &nbsp;}}这是我获取值的另一个类的“调用”private String[][] valores;Fetch process = new Fetch();&nbsp; process.execute();&nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; valores=process.get();&nbsp; &nbsp; &nbsp;} catch (ExecutionException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; pm.setText(String.valueOf(valores[Fetch.Key][0]));&nbsp; &nbsp;}&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答