gpt4 book ai didi

java - 为什么我的 ListView 不显示来自 SQLite 数据库的数据?

转载 作者:行者123 更新时间:2023-11-30 00:53:19 24 4
gpt4 key购买 nike

我是 android 开发的新手,所以这似乎是一个显而易见的问题,但我无法弄清楚。我正在尝试使用存储在 SQLite 数据库中的数据填充 ListView 。我已经设法填充了数据库。k 但是,我无法弄清楚为什么数据没有显示在 ListView 中。我的代码运行良好,没有错误。有人可以帮帮我吗?我将不胜感激!

主要 Activity .java:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;
import java.util.List;

public class MainActivity extends AppCompatActivity {

SimpleCursorAdapter simpleCursorAdapter;
SqlHelper sqlHelper;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

list = (ListView) findViewById(R.id.listView);
sqlHelper = new SqlHelper(this, null, null, 1);



/** CRUD Operations **/
// add Books
sqlHelper.addBook(new Book("Professional Android 4 Application Development", "Reto Meier", 4));
sqlHelper.addBook(new Book("Beginning Android 4 Application Development", "Wei-Meng Lee", 2));
sqlHelper.addBook(new Book("Programming Android", "Wallace Jackson", 3));
sqlHelper.addBook(new Book("Hello, Android", "Ben Wallace", 1));
// get all books
List<Book> list = sqlHelper.getAllBooks();
//sqlHelper.getAllBooks();
sqlHelper.getIds(list.get(0));

displayBookList();

}

private void displayBookList() {
try
{
Cursor cursor = sqlHelper.getBooks();
if (cursor == null)
{
return;
}
if (cursor.getCount() == 0)
{
return;
}
String[] columns = new String[] {
sqlHelper.KEY_ID,
sqlHelper.KEY_TITLE,
sqlHelper.KEY_AUTHOR,
sqlHelper.KEY_RATING
};
int[] boundTo = new int[] {
R.id.booknum,
R.id.booktitle,
R.id.bookauthor,
R.id.ratingBar
};
simpleCursorAdapter = new SimpleCursorAdapter(getApplicationContext(),
R.layout.each_book,
cursor,
columns,
boundTo,
0);
list.setAdapter(simpleCursorAdapter);
}
catch (Exception ex)
{

}
}

}

书.java:

public class Book {
private int id;
private String title;
private String author;
private int rating;

public Book() {
}

public Book(String title, String author, int rating) {
super();
this.title = title;
this.author = author;
this.rating = rating;
}

//getters & setters
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public int getRating() {
return rating;
}

public void setRating(int rating) {
this.rating = rating;
}

@Override
public String toString() {
return "Book [id=" + id + ", title=" + title + ", author=" + author + ", rating=" + rating + "]";
}
}

SQLHelper.java:

import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log;


public class SqlHelper extends SQLiteOpenHelper { // Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Books1DB"; // Books table name
private static final String TABLE_BOOKS = "books"; // Books Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_AUTHOR = "author";
public static final String KEY_RATING = "rating";

public SqlHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create book table
String CREATE_BOOK_TABLE = "CREATE TABLE books ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " + "title TEXT, " +
"author TEXT," + "rating INTEGER )";
// create books table
db.execSQL(CREATE_BOOK_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older books table if existed
db.execSQL("DROP TABLE IF EXISTS books"); // create fresh books table
this.onCreate(db);
}

public Cursor getBooks() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_BOOKS, new String[] {KEY_ID, KEY_TITLE,
KEY_AUTHOR, KEY_RATING}, null, null, null, null, null);
if(cursor != null)
{
cursor.moveToFirst();
return cursor;
}
else {
return null;
}
}

/*CRUD operations (create "add", read "get", update, delete) */
public void addBook(Book book){
Log.d("addBook", book.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_TITLE, book.getTitle()); // get title
values.put(KEY_AUTHOR, book.getAuthor()); // get author
values.put(KEY_RATING, book.getRating()); // get rating
// 3. insert
db.insert(TABLE_BOOKS, // table
null, //nullColumnHack
values); // key/value -> keys = column names/values
// 4. Close dbase
db.close();
}

// Get All Books
public List<Book> getAllBooks() {
List<Book> books = new LinkedList<Book>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_BOOKS;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build book and add it to list
Book book = null;
if (cursor.moveToFirst()) {
do {
book = new Book();
book.setId(Integer.parseInt(cursor.getString(0)));
book.setTitle(cursor.getString(1));
book.setAuthor(cursor.getString(2));
book.setRating(Integer.parseInt(cursor.getString(3)));
// Add book to books
books.add(book);
} while (cursor.moveToNext());
}
Log.d("getAllBooks()", books.toString());
return books; // return books
}

// Updating single book
public int updateBook(Book book) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put("title", book.getTitle()); // get title
values.put("author", book.getAuthor()); // get author
// 3. updating row
int i = db.update(TABLE_BOOKS, //table
values, // column/value
KEY_ID + " = ?", // selections
new String[]{String.valueOf(book.getId())}); //selection args
// 4. close dbase
db.close();
Log.d("UpdateBook", book.toString());
return i;
}

// Deleting single book
public void deleteBook(Book book) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
db.delete(TABLE_BOOKS, KEY_ID+" = ?",
new String[] { String.valueOf(book.getId()) });
//3. close
db.close();
Log.d("deleteBook", book.toString());
}

public int getIds(Book book) {
String selectQuery = "SELECT id FROM books";
SQLiteDatabase database = this.getReadableDatabase();
Cursor c = database.rawQuery(selectQuery, null);
c.moveToFirst();
int total = c.getCount();
String stringTotal = String.valueOf(total);
Log.d("Total Count:", stringTotal);
return total;
}

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center_horizontal" />

</LinearLayout>

each_book.java:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/booknum"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/booktitle"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/booknum"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/bookauthor"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/booktitle"
android:layout_toEndOf="@+id/booktitle" />

<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_below="@+id/bookauthor"
android:layout_alignParentStart="true" />

</RelativeLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shaydoe.bookreviews">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

最佳答案

CursorAdpater 需要一个名为 _id 的列。根据 Cursor 必须包含名为“_id”的列,否则此类将不起作用。 CursorAdapter .

因此您需要使游标有一个名为_id 的列。您可以通过几种方式做到这一点。例如在用于使用 AS _id 创建游标的查询中命名(重命名)或重命名实际列。我建议后者在这种情况下改变:

public static final String KEY_ID = "id";

public static final String KEY_ID = "_id";

关于java - 为什么我的 ListView 不显示来自 SQLite 数据库的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40581516/

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