gpt4 book ai didi

java - 如何将整数数组从 AsyncTask 获取到同一 Activity 中的另一个方法?

转载 作者:行者123 更新时间:2023-11-30 01:32:48 25 4
gpt4 key购买 nike

我有一个类 (ReadFromAsset),它从在线文本文件中获取两个 int 数组。我有意地将这些值发送到另一个 Activity 。现在,我将另一个 Activity 的代码 (OpenChart) 放入此 Activity 中,并希望将值从一个类发送到另一个类,从而替换 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

(...)

最佳答案

您可以将对其他类 OpenChat 的引用传递给您的异步类,在执行后将它们设置在其他类中。

所以

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

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

现在更改异步类的构造函数

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

关于java - 如何将整数数组从 AsyncTask 获取到同一 Activity 中的另一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com