以下是我用于Android应用程序开发的3个java类。我想在AddActivity中添加学生数据(姓名和电话号码),点击" Add"后存储在MainActivity页面中。我已经研究了这个并尝试使用数组,但我对于如何将代码将AddActivity中键入的数据发送到MainActivity页面的逻辑非常困惑。任何人都可以给我一个如何解决这个问题的指导,如果你能用另一种方式向我展示我正在尝试的方式,我真的很感激。我希望在每个" Add"之后,将数据以ListView格式存储在MainActivity中。我点击了AddActivity页面。希望有人能够指导我这样做。谢谢。
MainActivity.java - https://jsfiddle.net/eb1fprnn/
public class MainActivity extends AppCompatActivity {
ListView listView;
Button addStudent;
ArrayList<Student> students = new ArrayList<Student>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add();
}
public void add() {
Student student;
addStudent = (Button) findViewById(R.id.add);
addStudent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivity(intent);
}
});
}
}
AddActivity.java - https://jsfiddle.net/40k5mas2/
public class AddActivity extends AppCompatActivity {
EditText name, phone;
Button add;
int FphoneNumber;
String Fname;
ArrayList<Student> students;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
students = (ArrayList<Student>) getIntent().getSerializableExtra("AddNewStudent");
setContentView(R.layout.activity_add);
edit();
addStudent();
}
public void edit() {
name = (EditText) findViewById(R.id.StudentName);
phone = (EditText) findViewById(R.id.phone);
final Button addStudent = (Button) findViewById(R.id.AddStudent);
name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
addStudent.setEnabled(!name.getText().toString().trim().isEmpty());
Fname = name.getText().toString();
String phoneNumber = phone.getText().toString();
FphoneNumber = Integer.parseInt(phoneNumber);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public void addStudent() {
add = (Button) findViewById(R.id.AddStudent);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AddActivity.this, MainActivity.class);
intent.putExtra("studentName",name.getText().toString() );
intent.putExtra("phoneNumber",phone.getText().toString());
startActivity(intent);
Student student = new Student(Fname, FphoneNumber);
students.add(student);
}
});
}
public void addStudent(){
add = (Button) findViewById(R.id.AddStudent);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AddActivity.this,Record.class);
startActivity(intent);
}
});
}
Student.java - https://jsfiddle.net/gy0g7b0s/
public class Student {
String mName;
int mPhoneNumber;
public Student (String name, int number){
mName = name;
mPhoneNumber = number;
};
public String getmName() {
return mName;
}
public String getmName(String newName) {
return (this.mName = newName);
}
public int getmPhoneNumber() {
return this.mPhoneNumber;
}
public int getmPhoneNumber(int newPhoneNumber) {
return (this.mPhoneNumber = newPhoneNumber);
}
@Override
public String toString() {
return String.format("%s\t%f",this.mName, this.mPhoneNumber);
[1]:[主要活动页面的图像] http://imgur.com/a/pMWt4
[2]:[添加活动页面的图片] http://imgur.com/a/8YvVc
答案 0 :(得分:0)
您可以将它们存储为state.PassageReducer
或创建AddActivity构造函数并获取函数。
public static variable
在另一项活动中
String student name; /*add value to this variable @onCreate or wherever in your AddActivity*/
public class AddActivity(/*here to pass data to addactivity*/){
//
}
public String getName(){
return this.name;
}
您可以使用此逻辑传递数据。
编辑 但是如果你想用Intent传递数据,那么只需检查内容onCreate()
AddActivity ac = new AddActivity();
String someName = ac.getName();
答案 1 :(得分:0)
我能找到的最佳解决方案是,将数组列表声明为静态,如果类在同一个包中,您可以访问所需的位置。但是如果要存储这些值,请使用共享首选项。希望这可能有所帮助。