简体   繁体   中英

How to get the integer arrays from AsyncTask to another method in same activity?

I have a class ( ReadFromAsset ) that gets two int arrays from an online text file. I was sending those values to another activity with intent. Now I put the code ( OpenChart ) from the other activity in this activity and want to send the values from one class to another, replacing the intent. Can you help me with that?

public class ReadFromAssetActivity extends AppCompatActivity {
    private Button read_Button;
    Button btnParse;
    Spinner spinner;
    int[] force_l = null;
    int[] force_r = null;
    String path = "";
    private View mChart;

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

        // Getting reference to the button btn_chart
        Button btnChart = (Button) findViewById(R.id.btn_chart);

        // Defining click event listener for the button btn_chart
        View.OnClickListener clickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Draw the Chart
                openChart();
            } 
        };
        // Setting event click listener for the button btn_chart of the SensorGraph layout
        btnChart.setOnClickListener(clickListener);

        initializeUI();                    
    }

    private void initializeUI() {
        read_Button = (Button) findViewById(R.id.btnParse);
        read_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new ReadFromAsset().execute();
            }
        });
    }

    private class ReadFromAsset extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(getAssets().open(path)));
                String mLine = null;
                int count = 0;
                while ((mLine = reader.readLine()) != null) {
                    String[] integer_Strings = mLine.split(" ");
                    //System.out.println(Arrays.deepToString(integer_Strings));
                    if (count == 0) {
                        force_l = new int[integer_Strings.length];
                        for (int i = 0; i < integer_Strings.length; i++) {
                            force_l[i] = Integer.parseInt(integer_Strings[i]);
                        }
                        count++;
                    } else if (count == 1) {
                        force_r = new int[integer_Strings.length];
                        for (int i = 0; i < integer_Strings.length; i++) {
                            force_r[i] = Integer.parseInt(integer_Strings[i]);
                         }
                    }

                }

                System.out.println(Arrays.toString(force_l));
                System.out.println(Arrays.toString(force_r));

                Intent intent = new Intent(ReadFromAssetActivity.this, ReadFromAssetActivity.class);  // to change
                intent.putExtra("force_l", force_l);  // to change
                intent.putExtra("force_r", force_r);  // to change
                startActivity(intent);                // to change

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

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    }

    private void openChart() {

        Intent intent = getIntent();                   // to change

        int[] fl = intent.getIntArrayExtra("force_l"); // to change
        int[] fr = intent.getIntArrayExtra("force_r"); // to change

(...)

you could pass a reference to you other class OpenChat to your Async Class the on post execute set them in the other class.

so

class OpenChat{
    int[] firstArray;
    int[] secondArray
    //setters
public void setFirst(int[] fst){
    firstArray = fst;
 }

public void setSecond(int[] sec){
    firstArray = sec;
 }
}

now change the constructor of your Async Class

to

private mOpenChat; // <- add this instance var

ReadFromAsset(OpenChat openChat){
   this.mOpenChat = openChat;
}

now change onPostExecute to

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    mOpenChat.setFirst(force_l);
    mOpenChat.setSecond(force_r);
}

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