gpt4 book ai didi

java - 将 sqlite 数据从 ListView 传递到其他 Activity

转载 作者:行者123 更新时间:2023-12-01 10:32:56 25 4
gpt4 key购买 nike

我是 Android 编程新手

我尝试使用 onItemClick 监听器实现将数据从 Listview 传递到另一个 Activity 的 Intent,但我陷入困境。
按照我的代码:

ListviewActivity Activity 显示已保存书籍的列表,其中包含书名、书籍作者... ...

        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 用于获取信息 - 它就像存储在 SQL 数据库中的书籍的详细 View ,尤其是在 ListView 中显示。

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

当我运行应用程序时,它仅将数据从第一行传递到其他 Activity 。关于如何将每行的数据传递给第二个 Activity 的任何建议

最佳答案

假设您有不同的书籍布局,每个单元格中具有不同的数据,您应该使用 viewfindViewById:

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

关于java - 将 sqlite 数据从 ListView 传递到其他 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com