将listview中的sqlite数据传递给其他活动

时间:2016-01-25 14:18:22

标签: java android sqlite listview android-activity

我是Android编程的新手

我尝试使用onItemClick侦听器实现一个Intent将数据从我的Listview传递到另一个Activity,但是我被卡住了。
遵循我的代码:

ListviewActivity 活动显示已保存的书籍列表,包括booktitle,bookauthor ...    ...

        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数据库中,特别是在列表视图中显示。

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

当我运行应用程序时,它只将数据从第一行传递给另一个活动。有关如何将每行的数据传递给第二个活动的任何建议

2 个答案:

答案 0 :(得分:1)

假设您在每个单元格中都有不同的图书布局和不同的数据,则应使用..."restaurant":{"__type":"Pointer", "className":"Restaurants", "objectId":"kIIYe7Z0tD"},... view

findViewById

答案 1 :(得分:0)

使用

view.findViewById

而不是

findViewById

每个列表项都有findViewById(R.id.text_book_title)。所以它找到你的第一个。使用视图,您可以选择所点击项目的 text_book_title