未在listview Android中显示的数据库条目

时间:2013-10-27 18:39:42

标签: android database android-listview android-sqlite

我创建了一个数据库和列表。我目前有一个文本框和按钮,下面有列表。单击按钮后输入项目时,该项目将显示在列表中。但是,退出应用程序并重新输入之前输入的值时,不再显示在列表中。

这是列表代码:

package bookshelf.Android.Java;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import bookshelf.Android.Java.R;


public class Own extends Activity
{
    private EditText item;
    private ListView lv;
    private Toast toast;
    private Button addButton;
    ArrayList<String> items;
    ArrayAdapter<String> listad;
    List<String> books;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.own);
            CharSequence text = "Item added!";
            toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
            setUpView();



    }
    private void setUpView()
    {
        item = (EditText)this.findViewById(R.id.txtAmount);
        lv = (ListView)this.findViewById(R.id.listView1);
        addButton = (Button)this.findViewById(R.id.Add);
        items = new ArrayList<String>();
        items.clear();
        final DataBaseOwn dbo = new DataBaseOwn(Own.this);

        listad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);//need to create a way to get items to equal books
        lv.setAdapter(listad);
        addButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                addItemList();
                books = dbo.get(items);
                toast.show();
            }
        });
        item.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                // TODO Auto-generated method stub

                if (keyCode == KeyEvent.KEYCODE_ENTER)
                {
                    addItemList();
                }
                return true;
            }
        });
    }
    protected void addItemList()
    {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        if (isInputValid(item))
        {
            items.add(0,item.getText().toString());
            item.setText("");
            listad.notifyDataSetChanged();
        }
    }
    protected boolean isInputValid(EditText item2)
    {
        // TODO Auto-generatd method stub
        if (item2.getText().toString().trim().length()<1)
        {
            item2.setError("Please Enter Item");
            return false;
        } 
        else 
        {
            return true;
        }

    }
}

这是我创建的数据库

package bookshelf.Android.Java;

import java.util.ArrayList;
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;

public class DataBaseOwn extends SQLiteOpenHelper
{
    public static final int VERSION = 1;
    public static final String TABLE_NAME = "OwnList";
    public static final String DBNAME = "ownList.sqlite";
    public static final String ID = "id"; 
    public static final String BOOK = "book";
    static SQLiteDatabase db;

    public DataBaseOwn(Context context)
    {
        super(context, DBNAME, null, VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db)
    {
        // TODO Auto-generated method stub
        createDatabase(db);
    }
    private void createDatabase(SQLiteDatabase db)
    {
        db.execSQL("create table " + TABLE_NAME + "(" + ID + " integer primary key autoincrement not null, " + BOOK + " text " + ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    public Long Insert(String book)
    {
        ContentValues values = new ContentValues();
        values.put(BOOK, book);
        return db.insert(TABLE_NAME, null, values);
    }
    public List<String> get( ArrayList<String> item)
    {
        db = this.getWritableDatabase();
        String[] column = new String[] {BOOK};
        Cursor c = db.query(TABLE_NAME, column, null, null, null, null, null);
        List<String> result = item;
        int columnIndex = c.getColumnIndex(BOOK);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
        {
            result.add(c.getString(columnIndex));
        }
        return result;
    }
}

这是XML

<EditText
    android:id="@+id/txtAmount"
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:inputType="text"
    android:maxLength="200"
    android:hint="Enter_Author_and_Book_Title"
    android:clickable= "true" />

<Button
    android:id="@+id/Add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/txtAmount"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/txtAmount"
    android:text="Add" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/listView1"
    android:textIsSelectable="true" />

1 个答案:

答案 0 :(得分:1)

创建ArrayAdapter时,您的列表items是emtpy。您需要在列表中填入已插入数据库的所有项目。

final DataBaseOwn dbo = new DataBaseOwn(Own.this);

items = new ArrayList<String>();

//fill the list with all items from the database
items = dbo.get();

listad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(listad);

接下来,您需要更改DataBaseOwn.get()。 get() - 方法应仅返回数据库中存在的项目。

public List<String> get() {
    db = this.getWritableDatabase();

    String column = new String[] { BOOK };
    Cursor c = db.query(TABLE_NAME, column, null, null, null, null, null);

    List<String> result = new ArrayList<String>();

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        result.add(c.getString(0));
    }

    cursor.close();

    return result;
}

更新:

在您的活动中永远不会调用

db.insert(item)。所以这些项目只会添加到列表视图中,但不会插入到您的数据库中。从您的books = db.get();删除该行onClick(),并将此行dbo.insert(item.getText().toString()添加到您的addItemList()