ListView不显示数据库的内容

时间:2015-10-27 13:30:12

标签: android

我正在尝试从我的数据库中读取一些信息,但是当我运行我的应用程序时,它显示空白,我无法看到我出错的地方或我做错了什么

我已经更新了适配器功能,但我仍然坚持使用空列表,每次添加新行时列表都会增长但是没有数据进入列表视图。我也尝试过改变字体颜色,看看是不是问题,但它不是

我正在显示数据库内容的片段:

import android.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import java.sql.SQLException;

import app.rory.pocket_chef.Adapters.DBAdapter;
import app.rory.pocket_chef.Adapters.Recipes_Adapter;
import app.rory.pocket_chef.Adapters.Shopping_List_Adapter;
import app.rory.slidemenu.R;

/**
 * Created by Rory on 10/22/2014.
 */
public class shopping_List_Fragment extends Fragment {

    private Shopping_List_Adapter shopping;
    private DBAdapter db;


    View rootview;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.shopping_list_layout, container, false);
        return rootview;
    }

    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //opeing the DBApater
        db = new DBAdapter(getActivity());
        try {
            db.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //opening the shopping_list_adapter
        shopping = new Shopping_List_Adapter(getActivity());
        try {
            shopping.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Cursor cursor = shopping.getAllShoppingList();

        String[] columns = new String[] {Shopping_List_Adapter.NAME, Shopping_List_Adapter.QUANTITY};
        int[] to = new int[] {R.id.shopping_list};

        SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.row2, cursor, columns, to, 0);

        ListView shoppingList = (ListView)rootview.findViewById(R.id.shopping_list);
        shoppingList.setAdapter(myCursorAdapter);

    }


}

row2.xml

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

    <TextView
        android:id="@+id/item_name"
        android:paddingLeft="5dp"
        android:textSize="17dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <TextView
        android:id="@+id/item_quantity"
        android:paddingLeft="5dp"
        android:textSize="17dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

适配器

package app.rory.pocket_chef.Adapters;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.sql.SQLException;

/**
 * Created by Rory on 06/10/15.
 */
public class Shopping_List_Adapter {

    public static final String ROW_ID = "_id";
    public static final String NAME = "name";
    public static final String QUANTITY = "quantity";

    private static final String DATABASE_TABLE = "shopping_list";

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     *
     * @param ctx
     *            the Context within which to work
     */
    public Shopping_List_Adapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the shopping list database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     *
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException
     *             if the database could be neither opened or created
     */
    public Shopping_List_Adapter open() throws SQLException {
        this.mDbHelper = new DatabaseHelper(this.mCtx);
        this.mDb = this.mDbHelper.getWritableDatabase();
        return this;
    }

    /**
     * close return type: void
     */
    public void close() {
        this.mDbHelper.close();
    }

    /**
     * Create a new car. If the car is successfully created return the new
     * rowId for that item, otherwise return a -1 to indicate failure.
     *
     * @param name
     * @param quantity
     * @return rowId or -1 if failed
     */
    public long createItem(String name, String quantity){
        ContentValues initialValues = new ContentValues();
        initialValues.put(NAME, name);
        initialValues.put(QUANTITY, quantity);
        return this.mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
     * Delete the item with the given rowId
     *
     * @param rowId
     * @return true if deleted, false otherwise
     */
    public boolean deleteItem(long rowId) {

        return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$
    }

    /**
     * Return a Cursor over the list of all shopping list in the database
     *
     * @return Cursor over all shoppiing_list
     */
    public Cursor getAllShoppingList() {

        return this.mDb.rawQuery("SELECT * FROM shopping_list", null);
    }

    /**
     * Return a Cursor positioned at the car that matches the given rowId
     * @param rowId
     * @return Cursor positioned to matching car, if found
     * @throws SQLException if car could not be found/retrieved
     */
    public Cursor getItem(long rowId) throws SQLException {

        Cursor mCursor =

                this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, NAME,
                        QUANTITY}, ROW_ID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    /**
     * Update the shopping list.
     *
     * @param rowId
     * @param name
     * @param quantity
     * @return true if the note was successfully updated, false otherwise
     */
    public boolean updateShoppingList(long rowId, String name, String quantity,
                             String year){
        ContentValues args = new ContentValues();
        args.put(NAME, name);
        args.put(QUANTITY, quantity);

        return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0;
    }
}

2 个答案:

答案 0 :(得分:1)

更改

int[] to = new int[] {R.id.shopping_list};

int[] to = new int[] {R.id.item_name,R.id.item_quantity};

您必须将值放在行布局(Row2.xml)的TextViews中,而不是放在列表布局中。

答案 1 :(得分:0)

尝试完成this链接

中的步骤