You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击ListView时向另一个Activity的EditText传递多组数据

How to Pass Multiple StudentProfile Data to Another Activity on ListView Item Click

Great question! The cleanest way to pass all your student data at once is to make your StudentProfile class implement Parcelable (so you can pass the entire object via an Intent), then retrieve it in your main Activity to populate the EditTexts. Here's a step-by-step breakdown:


Step 1: Update StudentProfile to Implement Parcelable

First, we need to modify your StudentProfile class to implement Parcelable (this lets Android serialize the object for Intent transfer). We'll also add getter methods to access the private fields:

public class StudentProfile implements Parcelable {
    private int studIDnum;
    private String studFname;
    private String studLname;
    private String studMI;
    private String courseYr;

    // Constructor
    public StudentProfile(int studIDnum, String studFname, String studLname, String studMI, String courseYr) {
        this.studIDnum = studIDnum;
        this.studFname = studFname;
        this.studLname = studLname;
        this.studMI = studMI;
        this.courseYr = courseYr;
    }

    // Parcelable required methods
    protected StudentProfile(Parcel in) {
        studIDnum = in.readInt();
        studFname = in.readString();
        studLname = in.readString();
        studMI = in.readString();
        courseYr = in.readString();
    }

    public static final Creator<StudentProfile> CREATOR = new Creator<StudentProfile>() {
        @Override
        public StudentProfile createFromParcel(Parcel in) {
            return new StudentProfile(in);
        }

        @Override
        public StudentProfile[] newArray(int size) {
            return new StudentProfile[size];
        }
    };

    // Getters for all fields
    public int getStudIDnum() { return studIDnum; }
    public String getStudFname() { return studFname; }
    public String getStudLname() { return studLname; }
    public String getStudMI() { return studMI; }
    public String getCourseYr() { return courseYr; }

    @Override
    public int describeContents() { return 0; }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(studIDnum);
        dest.writeString(studFname);
        dest.writeString(studLname);
        dest.writeString(studMI);
        dest.writeString(courseYr);
    }
}

Step 2: Set Up ListView Item Click Listener (Source Activity/Dialog)

In the Activity or Dialog that contains your ListView, add an OnItemClickListener to handle when a student item is clicked. We'll get the selected StudentProfile object and pass it to your main Activity via an Intent:

If in an Activity:

ListView studentListView = findViewById(R.id.your_listview_id);
// Assume you have an adapter (like ArrayAdapter<StudentProfile>) set to the ListView

studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Get the selected student from the adapter
        StudentProfile selectedStudent = (StudentProfile) parent.getItemAtPosition(position);
        
        // Create Intent to launch your main Activity
        Intent intent = new Intent(YourSourceActivity.this, MainActivity.class);
        // Pass the Parcelable student object
        intent.putExtra("SELECTED_STUDENT", selectedStudent);
        startActivity(intent);
    }
});

If in a Dialog:

Use the Dialog's context to create the Intent, and dismiss the Dialog after launching the Activity:

studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        StudentProfile selectedStudent = (StudentProfile) parent.getItemAtPosition(position);
        
        Intent intent = new Intent(getContext(), MainActivity.class);
        intent.putExtra("SELECTED_STUDENT", selectedStudent);
        getContext().startActivity(intent);
        
        // Dismiss the dialog
        ((Dialog) getContext()).dismiss();
    }
});

Step 3: Populate EditTexts in Main Activity

In your main Activity (where the EditTexts are located), retrieve the StudentProfile object from the Intent and set each EditText's text with the corresponding field:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize your EditTexts (replace with your actual IDs)
    EditText etStudId = findViewById(R.id.et_student_id);
    EditText etFirstName = findViewById(R.id.et_first_name);
    EditText etLastName = findViewById(R.id.et_last_name);
    EditText etMiddleInitial = findViewById(R.id.et_middle_initial);
    EditText etCourseYear = findViewById(R.id.et_course_year);

    // Retrieve the student object from the Intent
    StudentProfile selectedStudent = getIntent().getParcelableExtra("SELECTED_STUDENT");

    // Populate EditTexts if the student object is not null
    if (selectedStudent != null) {
        etStudId.setText(String.valueOf(selectedStudent.getStudIDnum()));
        etFirstName.setText(selectedStudent.getStudFname());
        etLastName.setText(selectedStudent.getStudLname());
        etMiddleInitial.setText(selectedStudent.getStudMI());
        etCourseYear.setText(selectedStudent.getCourseYr());
    }
}

Why This Works:

  • Implementing Parcelable lets Android convert your StudentProfile object into a format that can be passed between components via Intents.
  • Passing the entire object is cleaner than passing each field as a separate extra (avoids messy code and reduces chance of errors).
  • The getters let you safely access the private fields of StudentProfile to populate the EditTexts.

内容的提问来源于stack exchange,提问作者Anne Leona

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:31:46