我有listview连接到数据库,当我点击listview中的任何项目时,我想为下一个活动中的特定项目获取数据

这是Listview我希望它从数据库中获取特定项目的数据并将其提取到第二个活动并将其放入Edittext中。我有listview连接到数据库,当我点击listview中的任何项目时,我想为下一个活动中的特定项目获取数据

@Override 

protected void onStart() {

super.onStart();

mArrayList.clear();

ArrayList<String> ar = new ArrayList<String>();

final List<DatabaseAccounts> data = dB.getallrows();

list = (ListView)findViewById(R.id.listView);

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

DatabaseAccounts dbAccount = data.get(position);

Intent intent = new Intent(getApplicationContext(),Diary2.class);

startActivity(intent);

}

});

for(DatabaseAccounts dbb : data){

mArrayList.add(dbb.getState());

}

mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,mArrayList);

list.setAdapter(mArrayAdapter);

}

回答:

首先,你需要实现Parcelable接口在类:

public class Student implements Parcelable { 

int id;

String name;

public Student(int id, String name) {

this.id = id;

this.name = name;

}

public int getId() {

return id;

}

public String getName() {

return name;

}

@Override

public int describeContents() {

// TODO Auto-generated method stub

return 0;

}

@Override

public void writeToParcel(Parcel dest, int arg1) {

// TODO Auto-generated method stub

dest.writeInt(id);

dest.writeString(name);

}

public Student(Parcel in) {

id = in.readInt();

name = in.readString();

}

public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {

public Student createFromParcel(Parcel in) {

return new Student(in);

}

public Student[] newArray(int size) {

return new Student[size];

}

};

}

而从调用活动

列表

ArrayList<Student> arraylist = new ArrayList<Student>(); 

代码

Intent intent = new Intent(this, SecondActivity.class); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("mylist", arraylist); intent.putExtras(bundle); this.startActivity(intent); 

代码名为活动

protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

Bundle bundle = getIntent().getExtras();

ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");

}

以上是 我有listview连接到数据库,当我点击listview中的任何项目时,我想为下一个活动中的特定项目获取数据 的全部内容, 来源链接: utcz.com/qa/262172.html

回到顶部