I can read and save a textfile to an Arraylist
with my code. Now I want to use this Arraylist
in other activities but I have problems with passing the Arraylist
to another class. I tried it with intent but it didnt work. I will add the codes of my program below.
我可以使用我的代码读取文本文件并将其保存到Arraylist。现在我想在其他活动中使用这个Arraylist但是我将Arraylist传递给另一个类时遇到了问题。我故意尝试了它,但它没有用。我将在下面添加我的程序代码。
Intent intent=new Intent(this, ergebnisse.class);
intent.putExtra("NEFZ", Nlist);
startActivity(intent);
I hope you can help me.
我希望你能帮助我。
My first activity:
我的第一个活动:
public class NEFZ2array extends AppCompatActivity implements Serializable {
public static void main(String[] args) {
FileReader file = null;
try {
file = new FileReader("NEFZ.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArrayList<Double> Nlist = new ArrayList<Double>();
int i=0;
Double d= null;
try {
BufferedReader input = new BufferedReader(file);
String s=null;
while((s=input.readLine())!=null) {
StringTokenizer st = new StringTokenizer(s,",");
while(st.hasMoreTokens()) {
try {
d = Double.parseDouble(st.nextToken());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Nlist.add(i, d);
}
}
input.close();
} catch(Exception e) {
e.printStackTrace();
}
for(double j:Nlist) {
System.out.println(j);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nefz2array);
}
}
My Receiver Activity:
我的接收器活动:
public class ergebnisse extends AppCompatActivity implements Serializable {
public Button button_ausfuerlicher;
public Button button_home;
public void init(){
button_ausfuerlicher = (Button) findViewById(R.id.button_ausfuerlicher);
button_ausfuerlicher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ergebnisse.this, ausfuehrlicher.class);
startActivity(intent);
}
});
}
public void init2(){
button_home = (Button) findViewById(R.id.button_home);
button_home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ergebnisse.this, MainActivity.class);
startActivity(intent);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ergebnisse);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
init();
init2();
}
}
1
You need to call getIntent() in your receiver activity.
您需要在接收器活动中调用getIntent()。
Intent intent = getIntent();
Intent intent = getIntent();
and then you can call
然后你可以打电话
intent.getExtras()
1
The entry point to an android application is typically the onCreate method.
android应用程序的入口点通常是onCreate方法。
I suspect that your main method (traditional java entry point) is not being run. You could try calling it from the onCreate, but should really rename it.
我怀疑你的主要方法(传统的java入口点)没有被运行。您可以尝试从onCreate调用它,但应该重命名它。
To receive an Intent, which should be sent as long as you call your main and add the Intent code, make sure the receiver overrides onReceive.
要接收Intent,只要您调用main并添加Intent代码就应该发送,请确保接收器覆盖onReceive。
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<Double> NEFZ = intent.getExtras().getString("NEFZ");
}
0
To receive the double array list in the receiver activity you can use:
要在接收器活动中接收双数组列表,您可以使用:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ergebnisse);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
// Get double list from sender activity
List<Double> doubleList = (ArrayList<Double>) getIntent().getSerializableExtra("NEFZ");
// TODO: Process your double list here
init();
init2();
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2018/08/28/6e6e65544c72c728c09b05a34cb93400.html。