gpt4 book ai didi

java - 如何将我在 EditText 框中键入的数据添加到数组中以在另一个 Activity 中列出?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:39 27 4
gpt4 key购买 nike

下面是我用于我的 android 应用程序开发的 3 个 java 类。单击“添加”后,我想从 AddActivity 添加学生数据(姓名和电话号码)以存储在 MainActivity 页面中。我对此进行了研究并尝试使用数组。但是我很困惑代码必须如何将 AddActivity 中键入的数据发送到 MainActivity 页面的逻辑。谁能给我指导如何解决这个问题,如果你能告诉我另一种方式而不是我正在尝试的方式,我将不胜感激。我希望在每次在 AddActivity 页面中单击“添加”后,数据以 ListView 格式存储在 MainActivity 中。我确实希望有人能够指导我这样做。谢谢。

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

}

学生.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] : [主要 Activity 页面图片] http://imgur.com/a/pMWt4

[2] : [添加 Activity 页面图片] http://imgur.com/a/8YvVc

最佳答案

如上所述,正确的方法是使用 startActivityForResult 方法。 Check this .

以及如何去做,太简单了!修改代码:

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);
startActivityForResult(intent,123);
}
});
}
}

并在同一个 Activity (MainActivity)中监听结果还建议您使用 parceler.org 库发送对象

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode== Activity.RESULT_OK && requestCode==123){
// perform your list addition operation here and notify the adapter for change
// the returned data comes in 'data' parameter and would recommend you to use parcels.org lib
// for sending parcelable pojo across activities and fragments.
list.add(Parcels.unwrap(data.getParcelableArrayExtra(YOUR_KEY)));
adapter.notifyDataSetChanged();
}
}

在您的 AddActivity 中,当您添加时,只需执行此操作。

    public void addStudent() {
// add the 'add' button view to the oncreatemethod
// add = (Button) findViewById(R.id.AddStudent);

add.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {
// Do not restart the activity that opened this activty
// this activity is anyways on top of the MainActivity. Just finish this activty setting the result

// Intent intent = new Intent(AddActivity.this, MainActivity.class);
// intent.putExtra("studentName",name.getText().toString() );
// intent.putExtra("phoneNumber",phone.getText().toString());
// startActivity(intent);

// How to do that?
Student student = new Student(Fname, FphoneNumber);
Intent intent = new Intent();
intent.putExtra(YOUR_KEY, Parcels.wrap(student));
// you can also do it without the parcels lib
// intent.putExtra("studentName",name.getText().toString() );
// intent.putExtra("phoneNumber",phone.getText().toString());

setResult(123,intent); // set the result code. it should be the same one as the one your listening on in MainAcitivty

// then just finish this activity.
finish();
// this calls the onActivtyResultMethod in MainActivity which furtther does the logic
// students.add(student);

}
});
}

那应该行得通!干杯!

关于java - 如何将我在 EditText 框中键入的数据添加到数组中以在另一个 Activity 中列出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42445890/

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