2016-11-14 15 views
1

これは明らかな質問のように思えるかもしれませんが、私はアンドロイド開発には新しいですが、私はそれを理解することはできません。私はSQLiteデータベースに格納されたデータをリストビューに取り込もうとしています。私はdatabase.kにデータを格納することができました。しかし、なぜデータがリストビューに表示されないのか分かりません。私のコードはエラーなく正常に動作します。誰かが私を助けてくれますか?私は本当にそれを感謝します!ListViewがSQLiteデータベースからデータを表示しないのはなぜですか?

MainActivity.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) 
     { 

     } 
    } 

} 

Book.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> 
+0

を:私は、その場合の変化で、後者をお勧めしたいです。場合によっては、アダプタが_idを必要とするように見えるので、これが問題になる可能性があります。 public static final String KEY_ID = "_id"; ' – MikeT

+0

上記のコメントは_ _カーソルに_idという名前の列を含める必要があります。 work._ [CursorAdapter](https://developer.android.com/reference/android/widget/CursorAdapter.html) – MikeT

+1

これは修正されました。どうもありがとうございます!! – shaydoe

答えて

1

CursorAdpaterには、_idという名前の列が必要です。 The Cursor must include a column named "_id" or this class will not work.CursorAdapter

したがって、カーソルに_idという名前の列があるようにする必要があります。あなたはいくつかの方法でこれを行うことができます。例えばAS _idを使用してカーソルを作成するか、実際の列の名前を変更するために使用されるクエリで名前を変更します(名前を変更します)。あなたがテーブルを作成するときに_id、それが動作するかどうかを確認するためのIDを変更してみてください

public static final String KEY_ID = "id";

public static final String KEY_ID = "_id";

0

次の場所にブレークポイントでデバッグを行ってください。
1)insert文。データが正常に挿入されたことを確認します。
2)select文で、データを含むカーソルがあるかどうかを確認します。
3)selectステートメントからデータを取得する場合、作成したリストビューアダプタに集中します。

+0

ありがとう!問題は上記のコメントで述べたように "_id"であるように見えました。しかし、助けてくれてありがとう! – shaydoe

関連する問題