如何从AsyncTask获取整数数组到同一活动中的另一个方法?

时间:2016-02-20 16:52:27

标签: java android android-asynctask

我有一个类( ReadFromAsset ),它从在线文本文件中获取两个int数组。我正在将这些值发送给另一个有意图的活动。现在我将代码( OpenChart )放在此活动的其他活动中,并希望将值从一个类发送到另一个类,从而替换intent。你能帮帮我吗?

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

(...)

1 个答案:

答案 0 :(得分:1)

你可以将对其他类OpenChat的引用传递给你的异步类,在post post上将它们设置在另一个类中。

所以

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

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

现在更改Async Class的构造函数

private mOpenChat; // <- add this instance var

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

现在将onPostExecute更改为

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