从另一个Activity单击EditText时,在ListView中传递数据

时间:2018-04-29 08:50:38

标签: android listview android-edittext

我希望在另一个活动中点击Listview到EditText时传递许多数据,

例如

我的活动/对话框列表视图。

First Image

编辑文本所在的主要活动

Second Image

这是我的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;
}

}

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

}

我的活动/ 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("");
        }
    });
}

}

先谢谢你们,对骚乱感到抱歉

1 个答案:

答案 0 :(得分:0)

如果所有数据都在您的StudentProfile类中,那么您应该明确地查看Parcelable界面。

通过实现此接口,您将能够通过Intent与其他活动共享StudentProfile对象(或任何其他对象,或任何其他实现Parcelable的复杂数据)。