[英]Passing sqlite data from listview to other activity
I am new to android programming 我是android编程的新手
I tried to implement a Intent passing Data from my Listview to another Activity using onItemClick listener, but I got stuck. 我试图使用onItemClick侦听器实现将数据从Listview传递到另一个Activity的Intent,但是我遇到了麻烦。
Following my codes: 按照我的代码:
ListviewActivity Activity showing a list of saved books with booktitle, bookauthor ... ... ListviewActivity活动显示已保存的书的列表,其中包含书名,书作者...
booklistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), BookInfoActivity.class);
editTextBooktitle = (TextView) findViewById(R.id.text_book_title);
String book_title = editTextBooktitle.getText().toString();
intent.putExtra(EXTRA_MSG1, book_title);
editTextBookauthor = (TextView) findViewById(R.id.text_book_author);
String bookauthor = editTextBookauthor.getText().toString();
intent.putExtra(EXTRA_MSG2, bookauthor);
editTextBookdate = (TextView) findViewById(R.id.text_book_date);
String bookdate = editTextBookdate.getText().toString();
intent.putExtra(EXTRA_MSG3, bookdate);
editTextBookrating = (TextView) findViewById(R.id.text_book_rating);
String bookrating = editTextBookrating.getText().toString();
intent.putExtra(EXTRA_MSG4, bookrating);
editTextBookshelf = (TextView) findViewById(R.id.text_book_shelf);
String bookshelf = editTextBookshelf.getText().toString();
intent.putExtra(EXTRA_MSG5, bookshelf);
startActivity(intent);
BookInfoActivity for getting the information - it is like a detail view of the book which is stored in a sql database, especially shown in the listview. BookInfoActivity用于获取信息-就像存储在sql数据库中的书的详细视图一样,特别是在列表视图中显示。
Intent intent = getIntent();
String book_title = intent.getStringExtra(BookDataListActivity.EXTRA_MSG1);
passedbooktitle = (EditText) findViewById(R.id.passed_booktitle);
passedbooktitle.setText(book_title);
String book_author = intent.getStringExtra(BookDataListActivity.EXTRA_MSG2);
passedbookauthor = (EditText) findViewById(R.id.passed_bookauthor);
passedbookauthor.setText(book_author);
String book_date = intent.getStringExtra(BookDataListActivity.EXTRA_MSG3);
passedbookdate = (EditText) findViewById(R.id.passed_bookdate);
passedbookdate.setText(book_date);
String book_rating = intent.getStringExtra(BookDataListActivity.EXTRA_MSG4);
passedbookrating = (EditText) findViewById(R.id.passed_bookrating);
passedbookrating.setText(book_rating);
String book_shelf = intent.getStringExtra(BookDataListActivity.EXTRA_MSG5);
passedbookshelf = (EditText) findViewById(R.id.passed_bookshelf);
passedbookshelf.setText(book_shelf);
When I run the application it only passes the data from the first row to the other activity. 当我运行应用程序时,它仅将数据从第一行传递到其他活动。 Any suggestions how I can pass the data for each row to the second activity 关于如何将每一行的数据传递给第二个活动的任何建议
Assuming that you have different book layouts with different data in every cell, you should use view
to findViewById
: 假设您在每个单元格中具有不同的书籍布局和不同的数据,则应使用view
来findViewById
:
booklistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), BookInfoActivity.class);
editTextBooktitle = (TextView) view.findViewById(R.id.text_book_title);
String book_title = editTextBooktitle.getText().toString();
intent.putExtra(EXTRA_MSG1, book_title);
editTextBookauthor = (TextView) view.findViewById(R.id.text_book_author);
String bookauthor = editTextBookauthor.getText().toString();
intent.putExtra(EXTRA_MSG2, bookauthor);
editTextBookdate = (TextView) view.findViewById(R.id.text_book_date);
String bookdate = editTextBookdate.getText().toString();
intent.putExtra(EXTRA_MSG3, bookdate);
editTextBookrating = (TextView) view.findViewById(R.id.text_book_rating);
String bookrating = editTextBookrating.getText().toString();
intent.putExtra(EXTRA_MSG4, bookrating);
editTextBookshelf = (TextView) view.findViewById(R.id.text_book_shelf);
String bookshelf = editTextBookshelf.getText().toString();
intent.putExtra(EXTRA_MSG5, bookshelf);
startActivity(intent);
}
}
Use 采用
view.findViewById
instead of 代替
findViewById
Every list item has findViewById(R.id.text_book_title). 每个列表项都有findViewById(R.id.text_book_title)。 So it finds you the first one. 因此,它找到了您的第一个。 By using the view you select the text_book_title of the item clicked. 通过使用视图,可以选择所单击项目的text_book_title 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.