gpt4 book ai didi

android - 更新和删除 ListView 中的行

转载 作者:行者123 更新时间:2023-12-03 18:11:57 25 4
gpt4 key购买 nike

我知道这个问题并不新鲜,但我在这里找到的所有其他示例都不起作用。这就是我给你写信的原因。

我创建了一个图书应用程序,您可以在其中保存来自 googlebooks 的图书数据,并将数据保存到 sqlite 数据库中。 bookdata 将显示在 ListView 中。
如果单击一行,则该行的所有数据将显示在另一个 Activity 的 EdittExts 中 - 看起来像详细的书籍 View 。
一些示例图片:Listview ; detailed bookview

现在我想给用户编辑这些 bookinfos 的可能性。
我实现了一些代码来更新和删除信息,并从 Logcat 获取信息,即创建了 Db,但是当我返回 ListView 时,没有更新或删除数据。

我使用了来自 youtube 的名为“Android Studio 教程 - 37 - 更新数据库”的视频来转换为我的解决方案

也许你可以帮助我。

按照我的图书信息代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_info);
bookDBHelper = new BookDBHelper(this);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
TextView myTextView = (TextView) findViewById(R.id.yourbookinfo);
myTextView.setTypeface(myTypeface);

btn_update = (Button) findViewById(R.id.button_update);

Intent intent = getIntent();

String secret_title = intent.getStringExtra(BookDataListActivity.EXTRA_MSG1);
Secret_editText_title = (EditText) findViewById(R.id.secret_edittext_title);
Secret_editText_title.setText(secret_title);

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

}


public void updateBookInfo(View view){
bookDBHelper = new BookDBHelper(getApplicationContext());
sqLiteDatabaseBooks = bookDBHelper.getWritableDatabase();

secret_editText_titel = Secret_editText_title.getText().toString();

String booktitle, bookauthor, bookdate, bookrating, bookshelf;
booktitle = passedbooktitle.getText().toString();
bookauthor = passedbookauthor.getText().toString();
bookdate = passedbookdate.getText().toString();
bookrating = passedbookrating.getText().toString();
bookshelf = passedbookshelf.getText().toString();
int count = bookDBHelper.updateEditedBookInfo(secret_editText_titel,
booktitle,bookauthor, bookdate, bookrating, bookshelf, sqLiteDatabaseBooks);
Toast.makeText(getApplicationContext(), count+" book updated", Toast.LENGTH_LONG).show();
finish();

}

public void deleteBook(View view){
bookDBHelper = new BookDBHelper(getApplicationContext());
sqLiteDatabaseBooks = bookDBHelper.getWritableDatabase();
bookDBHelper.deleteBookInformation(secret_editText_titel, sqLiteDatabaseBooks);
Toast.makeText(getBaseContext(), "Book deleted", Toast.LENGTH_LONG).show();
}

对于我的 BookDBHelper:
public int updateEditedBookInfo(String old_title, String new_title, String new_author,
String new_date, String new_rating,
String new_shelf, SQLiteDatabase sqLiteDatabase){
ContentValues contentValues = new ContentValues();
contentValues.put(BookContent.NewBookInfo.BOOK_TITLE, new_title);
contentValues.put(BookContent.NewBookInfo.BOOK_AUTHOR, new_author);
contentValues.put(BookContent.NewBookInfo.BOOK_DATE, new_date);
contentValues.put(BookContent.NewBookInfo.BOOK_RATING, new_rating);
contentValues.put(BookContent.NewBookInfo.BOOK_SHELF, new_shelf);

String selection = BookContent.NewBookInfo.BOOK_TITLE + " = ?";
String [] selection_args = {old_title};
int count = sqLiteDatabase.update(BookContent.NewBookInfo.TABLE_NAME_BOOKS, contentValues, selection, selection_args);
return count;
}

public void deleteBookInformation(String book_title,SQLiteDatabase sqLiteDatabase){
String selection = BookContent.NewBookInfo.BOOK_TITLE+ " =?";
String [] selection_args = {book_title};
sqLiteDatabase.delete(BookContent.NewBookInfo.TABLE_NAME_BOOKS, selection, selection_args);
}

按要求编辑 activity_book_info.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kasutwentyseven.gui4selfshelf.Books.BookInfoActivity"
android:background="@color/Background">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/yourbookinfo"
android:id="@+id/yourbookinfo"
android:textSize="25dp"
android:textColor="@color/Textcolor"
android:gravity="center_horizontal"
android:textStyle="bold"
/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/yourbookinfo"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout5">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/passed_bookimage" />

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passed_booktitle" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passed_bookauthor" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passed_bookdate" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passed_bookrating" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passed_bookshelf" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout5"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout6">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_update"
android:id="@+id/button_update"
android:onClick="updateBookInfo"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_delete"
android:id="@+id/btn_delete_book"
android:onClick="deleteBook"/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout6">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/secret_edittext_title" />
</LinearLayout>
</RelativeLayout>

最佳答案

在@0x0nosugar 的帮助下(谢谢)
我找到了一个解决方案:

到更新书信息:
我补充说:

Intent intent = new Intent(getApplicationContext(), BookDataListActivity.class);
startActivity(intent);

并删除:
    public void DeleteData(){
btn_delete_book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer deletedRows = bookDBHelper.deleteDate(passedbooktitle.getText().toString());
if (deletedRows > 0) {
Toast.makeText(getApplicationContext(), " Book deleted", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), BookDataListActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), " Book not deleted", Toast.LENGTH_LONG).show();
}
}

});

在 BookDBHelper:
    public Integer deleteDate (String book_title){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String selection = BookContent.NewBookInfo.BOOK_TITLE+ " = ?";
String[] selection_args = {book_title};
return sqLiteDatabase.delete(BookContent.NewBookInfo.TABLE_NAME_BOOKS,selection, selection_args);
}

列表 Activity 中的方法:
    @Override
public void onResume(){
super.onResume();
bookListDataAdapter.notifyDataSetChanged();

}

关于android - 更新和删除 ListView 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35042101/

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