简体   繁体   English

单击另一个活动中的EditText时,在ListView中传递数据

[英]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 , 我想在另一个视图中单击Listview到EditText时传递许多数据,

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 这是我的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 我的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 我的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 我的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. 如果所有的数据在你的StudentProfile类,那么你应该definitly看一看的Parcelable接口。

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. 通过实现此接口,您将能够通过Intent与其他活动共享StudentProfile对象(或任何其他对象,或者更确切地说,是实现Parcelable的任何其他复杂数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮时如何将数据从editText.text传递到Xamarin Android中的另一个活动 - How to pass data from editText.text to another activity in Xamarin Android when button is clicked 将ListView中的EditText值传递给另一个Activity - Pass EditText value from ListView to another Activity 将数据从列表视图传递到另一个活动的edittext - Passing data from listview to edittext of another activity 将数据从 listView 传递到另一个 Activity EditText - Passing data from listView to another Activity EditText 如何将多个数据从 EditText(Main Activity) 传递到 ListView(Another Activity) - How to pass multiple data from EditText(Main Activity) to ListView(Another Activity ) 将 ListView 值传递给另一个 Activity 中的多个 EditText - Pass ListView values to multiple EditText in another Activity 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? 将数据从edittext添加到listview另一个活动android - add data to listview from edittext another activity android 将单击的Listview的特定值传递给另一个活动 - Pass the particular value of clicked Listview to another activity 将数据从listview中的单击项传递到另一个活动时出错 - Error while passing data from clicked item in listview to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM