简体   繁体   中英

Pass Data in ListView when clicked to EditText from another Activity

I want to pass many data when clicked in Listview to EditText in another acitivty ,

For Example

My Activity/Dialog where the listview.

First Image

My Main Activity where the edit text is located

Second Image

This is my StudentProfile.java

public class StudentProfile {

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


//setter and getter


public int getStudIDnum() {
    return studIDnum;
}

public void setStudIDnum(int studIDnum) {
    this.studIDnum = studIDnum;
}

public String getStudFname() {
    return studFname;
}

public void setStudFname(String studFname) {
    this.studFname = studFname;
}

public String getStudLname() {
    return studLname;
}

public void setStudLname(String studLname) {
    this.studLname = studLname;
}

public String getStudMI() {
    return studMI;
}

public void setStudMI(String studMI) {
    this.studMI = studMI;
}

public String getCourseYr() {
    return courseYr;
}

public void setCourseYr(String courseYr) {
    this.courseYr = courseYr;
}

}

My StudentProfileAdapter.java

public class StudentProfileListAdapter extends BaseAdapter {


private Context mContext;
private List<StudentProfile> mStudentList;

public StudentProfileListAdapter(Context mContext, List<StudentProfile> mStudentList) {
    this.mContext = mContext;
    this.mStudentList = mStudentList;
}

@Override
public int getCount() {
    return mStudentList.size();
}

@Override
public Object getItem(int position) {
    return mStudentList.get(position);
}

@Override
public long getItemId(int position) {
    return mStudentList.get(position).getStudIDnum();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {



    View v = View.inflate(mContext, R.layout.item_listviewstudent, null);
    TextView tvStudID = (TextView) v.findViewById(R.id.StudID);
    TextView tvStudF = (TextView) v.findViewById(R.id.StudF);
    TextView tvStudMI = (TextView) v.findViewById(R.id.StudM);
    TextView tvStudL = (TextView) v.findViewById(R.id.StudL);
    TextView tvStudCY = (TextView) v.findViewById(R.id.StudCY);

    //set text for textview
    tvStudID.setText(String.valueOf(mStudentList.get(position).getStudIDnum()));
    tvStudF.setText(mStudentList.get(position).getStudFname());
    tvStudMI.setText(mStudentList.get(position).getStudMI());
    tvStudL.setText(mStudentList.get(position).getStudLname());
    tvStudCY.setText(mStudentList.get(position).getCourseYr());

    return v;
}

}

My DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATBASE_NAME="StudentViolationDatabase.db";

private Context mContext;
private SQLiteDatabase mDatabase;


public DatabaseHelper(Context context) {
    super(context, DATBASE_NAME,null,1);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


public void openDatabase(){
    String dbPath = mContext.getDatabasePath(DATBASE_NAME).getPath();
    if(mDatabase != null && mDatabase.isOpen()){
        return;
    }
    mDatabase = SQLiteDatabase.openDatabase(dbPath,null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase(){
    if(mDatabase!=null){
        mDatabase.close();
    }
}

public List<StudentProfile> getListStudent() {
    StudentProfile studentProfile = null;
    List<StudentProfile> studentProfileList = new ArrayList<>();
    openDatabase();
    Cursor cursor = mDatabase.rawQuery("SELECT StudIDnum , StudFname , StudLname , StudMI , StudCourseYr FROM StudentProfile" , null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        studentProfile = new StudentProfile(cursor.getInt(0), cursor.getString(1) , cursor.getString(2) , cursor.getString(3) , cursor.getString(4));
        studentProfileList.add(studentProfile);
        cursor.moveToNext();
    }

    cursor.close();
    closeDatabase();
    return studentProfileList;
}

}

My Activity/Dialog.java

public class Dialog2 extends AppCompatActivity {

private ListView lvStudent;
private StudentProfileListAdapter adapter;
private List<StudentProfile> mStudentList;
private DatabaseHelper mDBHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_dialog2);


    Button btnCancel = (Button)findViewById(R.id.btnCancel);
    Button btnClear = (Button)findViewById(R.id.btnClear);
    final EditText eSearch = (EditText)findViewById(R.id.EditSearch);

    lvStudent = (ListView)findViewById(R.id.listviewstudent);
    mDBHelper = new DatabaseHelper(this);


    //Get Student list in db when db exists
    mStudentList = mDBHelper.getListStudent();
    //Init adapter
    adapter = new StudentProfileListAdapter(this, mStudentList);
    //set adapter for listview
    lvStudent.setAdapter(adapter);



    lvStudent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });



    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog2.this.finish();
        }
    });

    btnClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            eSearch.setText("");
        }
    });
}

}

Thanks in Advance guys , sorry for the disturbance

If all the data is in your StudentProfile class then you should definitly take a look at the Parcelable interface.

By implementing this interface you will be able to share a StudentProfile object (or any other object or rather any other complex data that implements Parcelable) with other activites via Intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM