Java Android ListView不显示数据库信息

Java Android ListView不显示数据库信息,java,android,database,sqlite,listview,Java,Android,Database,Sqlite,Listview,以上附件是该问题的截图。不会使用数据库的信息更新: XML文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/catChooser" android:layout_width

以上附件是该问题的截图。不会使用数据库的信息更新:

XML文件

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

    <Spinner
        android:id="@+id/categoryChoose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:entries="@array/catergory_arrays"
        android:gravity="right" />

    <ListView
        android:id="@+id/dbListView"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginTop="10dip" >

    </ListView>

    <Button
        android:id="@+id/scanCurrent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dip"
        android:text="@string/scan" />

    <Button
        android:id="@+id/editItemCurrent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"
        android:text="@string/editItem" />

</LinearLayout>
package com.example.fooditemmonitor;

import java.util.ArrayList;

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

public final class ItemDatabase {

    // the Activity or Application that is creating an object from this class.
    Context context;

    // a reference to the database used by this application/object
    private SQLiteDatabase db;

    // These constants are specific to the database.
    private final String DATABASE_NAME = "ItemDatabase.sqlite";
    private final int DATABASE_VERSION = 3;

    // These constants are specific to the database table.
    private final String TABLE_NAME = "foodItems";
    private final String COLUMN_NAME_ENTRY_ID = "entryid";
    private final String COLUMN_NAME_BARCODE = "barcode";
    private final String COLUMN_NAME_TITLE = "title";
    private final String COLUMN_NAME_QUANTITY = "quantity";
    private final String COLUMN_NAME_DATE = "date";
    String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
    String SQL_CREATE_TABLE = "create table " + TABLE_NAME + " ("
            + COLUMN_NAME_ENTRY_ID
            + " integer primary key autoincrement not null," + COLUMN_NAME_DATE
            + " date," + COLUMN_NAME_BARCODE + " text," + COLUMN_NAME_TITLE
            + " text," + COLUMN_NAME_QUANTITY + " int" + ");";

    public ItemDatabase(Context context) {
        this.context = context;

        // create or open the database
        ItemDatabaseHelper helper = new ItemDatabaseHelper(context);
        this.db = helper.getWritableDatabase();
    }

    public void addRow(String rowStringOne, String rowStringTwo,
            String rowStringThree, int rowIntFour) {
        // this is a key value pair holder used by android's SQLite functions
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME_DATE, rowStringOne);
        values.put(COLUMN_NAME_BARCODE, rowStringTwo);
        values.put(COLUMN_NAME_TITLE, rowStringThree);
        values.put(COLUMN_NAME_QUANTITY, rowIntFour);

        // ask the database object to insert the new data
        try {
            db.insert(TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e("DB ERROR", e.toString());
            e.printStackTrace();
        }
    }

    public void updateRow(long rowID, String rowStringOne, String rowStringTwo,
            String rowStringThree, int rowIntFour) {
        // this is a key value pair holder used by android's SQLite functions
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME_DATE, rowStringOne);
        values.put(COLUMN_NAME_BARCODE, rowStringTwo);
        values.put(COLUMN_NAME_TITLE, rowStringThree);
        values.put(COLUMN_NAME_QUANTITY, rowIntFour);

        // ask the database object to update the database row of given rowID
        try {
            db.update(TABLE_NAME, values, COLUMN_NAME_ENTRY_ID + "=" + rowID,
                    null);
        } catch (Exception e) {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
    }

    public void deleteRow(long rowID) {
        // ask the database manager to delete the row of given id
        try {
            db.delete(TABLE_NAME, COLUMN_NAME_ENTRY_ID + "=" + rowID, null);
        } catch (Exception e) {
            Log.e("DB ERROR", e.toString());
            e.printStackTrace();
        }
    }

    public ArrayList<ArrayList<Object>> getAllRowsAsArrays() {
        // create an ArrayList that will hold all of the data collected from
        // the database.
        ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
        // this is a database call that creates a "cursor" object.
        // the cursor object store the information collected from the
        // database and is used to iterate through the data.
        Cursor cursor;

        try {
            // ask the database object to create the cursor.

            cursor = db.query(TABLE_NAME, new String[] { COLUMN_NAME_DATE,
                    COLUMN_NAME_TITLE, COLUMN_NAME_QUANTITY }, null, null,
                    null, null, COLUMN_NAME_TITLE + " DESC");

            // move the cursor's pointer to position zero.
            cursor.moveToFirst();

            // if there is data after the current cursor position, add it to the
            // ArrayList.
            while (cursor.moveToNext()) {
                // your content

                ArrayList<Object> dataList = new ArrayList<Object>();

                dataList.add(cursor.getColumnIndex(COLUMN_NAME_DATE));
                dataList.add(cursor.getColumnIndex(COLUMN_NAME_TITLE));
                dataList.add(cursor.getColumnIndex(COLUMN_NAME_QUANTITY));

                dataArrays.add(dataList);
            }
        } catch (SQLException e) {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
        // return the ArrayList that holds the data collected from the database.
        return dataArrays;
    }

    public class ItemDatabaseHelper extends SQLiteOpenHelper {
        public ItemDatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            // execute the query string to the database.
            db.execSQL(SQL_CREATE_TABLE);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // This database is only a cache for online data, so its upgrade
            // policy is to simply to discard the data and start over
            db.execSQL(SQL_DELETE_ENTRIES);
            onCreate(db);
        }
    }
}

CurrentItems-应显示当前数据库行

package com.example.fooditemmonitor;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class CurrentItems extends Activity {

    ItemDatabase db;
    Context context;
    Button addButton, editButton;
    ListView listView;

    // the table that displays the data

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_inventory);

        db = new ItemDatabase(this);

        // create references and listeners for the GUI interface
        setupViews();

        // make the buttons clicks perform actions
        addButtonListeners();

        displaySearch();

    }

    private void setupViews() {
        // bring up current database items
        listView = (ListView) findViewById(R.id.dbListView);

        // THE BUTTONS
        addButton = (Button) findViewById(R.id.scanCurrent);
        editButton = (Button) findViewById(R.id.editItemCurrent);
    }

    private void addButtonListeners() {

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CurrentItems.this, AddItem.class));

            }
        });

        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CurrentItems.this, EditItems.class));

            }
        });
    }

    private void displaySearch() {
        // TODO Auto-generated method stub
        ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();
        final ArrayList<String> items = new ArrayList<String>();
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);
        for (int position = 0; position < data.size(); position++) {
            ArrayList<Object> row = data.get(position);
            items.add("\nDate:" + row.get(0).toString() + "    Title:"
                    + row.get(1).toString() + "\n" + "Quantity:"
                    + Integer.parseInt(row.get(2).toString()) + "\n");
            aa.notifyDataSetChanged();
        }
        listView.setAdapter(aa);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}
package com.example.foodeTommonitor;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
公共类CurrentItems扩展活动{
项目数据库数据库;
语境;
按钮addButton,editButton;
列表视图列表视图;
//显示数据的表
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.current_inventory);
db=新的项目数据库(此);
//为GUI界面创建引用和侦听器
setupview();
//使按钮单击执行操作
addButtonListeners();
displaySearch();
}
私有void setupViews(){
//调出当前数据库项
listView=(listView)findViewById(R.id.dbListView);
//按钮
addButton=(按钮)findViewById(R.id.scanCurrent);
editButton=(按钮)findViewById(R.id.editItemCurrent);
}
私有void addButtonListeners(){
addButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startActivity(新意图(CurrentItems.this,AddItem.class));
}
});
editButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startActivity(新意图(CurrentItems.this、EditItems.class));
}
});
}
私有void displaySearch(){
//TODO自动生成的方法存根
ArrayList data=db.getAllrowsaArrays();
最终ArrayList项=新ArrayList();
最终阵列适配器aa;
aa=新阵列适配器(此,
android.R.layout.simple_list_item_1,items);
对于(int position=0;position
项目数据库

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

    <Spinner
        android:id="@+id/categoryChoose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:entries="@array/catergory_arrays"
        android:gravity="right" />

    <ListView
        android:id="@+id/dbListView"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginTop="10dip" >

    </ListView>

    <Button
        android:id="@+id/scanCurrent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dip"
        android:text="@string/scan" />

    <Button
        android:id="@+id/editItemCurrent"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"
        android:text="@string/editItem" />

</LinearLayout>
package com.example.fooditemmonitor;

import java.util.ArrayList;

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

public final class ItemDatabase {

    // the Activity or Application that is creating an object from this class.
    Context context;

    // a reference to the database used by this application/object
    private SQLiteDatabase db;

    // These constants are specific to the database.
    private final String DATABASE_NAME = "ItemDatabase.sqlite";
    private final int DATABASE_VERSION = 3;

    // These constants are specific to the database table.
    private final String TABLE_NAME = "foodItems";
    private final String COLUMN_NAME_ENTRY_ID = "entryid";
    private final String COLUMN_NAME_BARCODE = "barcode";
    private final String COLUMN_NAME_TITLE = "title";
    private final String COLUMN_NAME_QUANTITY = "quantity";
    private final String COLUMN_NAME_DATE = "date";
    String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
    String SQL_CREATE_TABLE = "create table " + TABLE_NAME + " ("
            + COLUMN_NAME_ENTRY_ID
            + " integer primary key autoincrement not null," + COLUMN_NAME_DATE
            + " date," + COLUMN_NAME_BARCODE + " text," + COLUMN_NAME_TITLE
            + " text," + COLUMN_NAME_QUANTITY + " int" + ");";

    public ItemDatabase(Context context) {
        this.context = context;

        // create or open the database
        ItemDatabaseHelper helper = new ItemDatabaseHelper(context);
        this.db = helper.getWritableDatabase();
    }

    public void addRow(String rowStringOne, String rowStringTwo,
            String rowStringThree, int rowIntFour) {
        // this is a key value pair holder used by android's SQLite functions
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME_DATE, rowStringOne);
        values.put(COLUMN_NAME_BARCODE, rowStringTwo);
        values.put(COLUMN_NAME_TITLE, rowStringThree);
        values.put(COLUMN_NAME_QUANTITY, rowIntFour);

        // ask the database object to insert the new data
        try {
            db.insert(TABLE_NAME, null, values);
        } catch (Exception e) {
            Log.e("DB ERROR", e.toString());
            e.printStackTrace();
        }
    }

    public void updateRow(long rowID, String rowStringOne, String rowStringTwo,
            String rowStringThree, int rowIntFour) {
        // this is a key value pair holder used by android's SQLite functions
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME_DATE, rowStringOne);
        values.put(COLUMN_NAME_BARCODE, rowStringTwo);
        values.put(COLUMN_NAME_TITLE, rowStringThree);
        values.put(COLUMN_NAME_QUANTITY, rowIntFour);

        // ask the database object to update the database row of given rowID
        try {
            db.update(TABLE_NAME, values, COLUMN_NAME_ENTRY_ID + "=" + rowID,
                    null);
        } catch (Exception e) {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
    }

    public void deleteRow(long rowID) {
        // ask the database manager to delete the row of given id
        try {
            db.delete(TABLE_NAME, COLUMN_NAME_ENTRY_ID + "=" + rowID, null);
        } catch (Exception e) {
            Log.e("DB ERROR", e.toString());
            e.printStackTrace();
        }
    }

    public ArrayList<ArrayList<Object>> getAllRowsAsArrays() {
        // create an ArrayList that will hold all of the data collected from
        // the database.
        ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
        // this is a database call that creates a "cursor" object.
        // the cursor object store the information collected from the
        // database and is used to iterate through the data.
        Cursor cursor;

        try {
            // ask the database object to create the cursor.

            cursor = db.query(TABLE_NAME, new String[] { COLUMN_NAME_DATE,
                    COLUMN_NAME_TITLE, COLUMN_NAME_QUANTITY }, null, null,
                    null, null, COLUMN_NAME_TITLE + " DESC");

            // move the cursor's pointer to position zero.
            cursor.moveToFirst();

            // if there is data after the current cursor position, add it to the
            // ArrayList.
            while (cursor.moveToNext()) {
                // your content

                ArrayList<Object> dataList = new ArrayList<Object>();

                dataList.add(cursor.getColumnIndex(COLUMN_NAME_DATE));
                dataList.add(cursor.getColumnIndex(COLUMN_NAME_TITLE));
                dataList.add(cursor.getColumnIndex(COLUMN_NAME_QUANTITY));

                dataArrays.add(dataList);
            }
        } catch (SQLException e) {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
        // return the ArrayList that holds the data collected from the database.
        return dataArrays;
    }

    public class ItemDatabaseHelper extends SQLiteOpenHelper {
        public ItemDatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            // execute the query string to the database.
            db.execSQL(SQL_CREATE_TABLE);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // This database is only a cache for online data, so its upgrade
            // policy is to simply to discard the data and start over
            db.execSQL(SQL_DELETE_ENTRIES);
            onCreate(db);
        }
    }
}
package com.example.foodeTommonitor;
导入java.util.ArrayList;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.SQLException;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入android.util.Log;
公共最终类项目数据库{
//正在从此类创建对象的活动或应用程序。
语境;
//此应用程序/对象使用的数据库的引用
专用数据库数据库;
//这些常量特定于数据库。
私有最终字符串数据库\u NAME=“ItemDatabase.sqlite”;
私有最终int数据库_版本=3;
//这些常量特定于数据库表。
私有最终字符串表\u NAME=“foodItems”;
私有最终字符串列\u NAME\u ENTRY\u ID=“entryid”;
私有最终字符串列\u NAME\u BARCODE=“BARCODE”;
私有最终字符串列\u NAME\u TITLE=“TITLE”;
私有最终字符串列\u NAME\u QUANTITY=“QUANTITY”;
私有最终字符串列\u NAME\u DATE=“DATE”;
字符串SQL\u DELETE\u ENTRIES=“如果存在,则删除表格”+表格名称;
字符串SQL\u CREATE\u TABLE=“CREATE TABLE”+TABLE\u NAME+”(“
+列\u名称\u条目\u ID
+“整型主键自动递增不为空,”+列\u名称\u日期
+“日期”+“列名称”+“条形码”+“文本”+“列名称”\u标题
+“文本,“+列名称\数量+”整数“+”;
公共项目数据库(上下文){
this.context=上下文;
//创建或打开数据库
ItemDatabaseHelper=新的ItemDatabaseHelper(上下文);
this.db=helper.getWritableDatabase();
}
public void addRow(字符串rowStringOne、字符串rowStringTwo、,
字符串行三,整数行四){
//这是android的SQLite函数使用的键值对持有者
ContentValues=新的ContentValues();
value.put(列\名称\日期,rowStringOne);
value.put(列\名称\条形码,第二行);
value.put(列\名称\标题,第三行);
value.put(列\名称\数量,第四行);
//要求数据库对象插入新数据
试一试{
db.insert(表名称,空,值);
}捕获(例外e){
Log.e(“DB ERROR”,e.toString());
e、 printStackTrace();
}
}
public void updateRow(长rowID、字符串rowStringOne、字符串rowStringTwo、,
字符串行三,整数行四){
//这是android的SQLite函数使用的键值对持有者
ContentValues=新的ContentValues();
value.put(列\名称\日期,rowStringOne);
value.put(列\名称\条形码,第二行);
value.put(列名称