0

I have created a database and list. I currently have a textbox and button with the list underneath it. When an item is entered after clicking a button, the item shows in the list. However, when exiting the app and reentering the values that were entered before are not showing in the list anymore.

This is the list code:

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;
        }

    }
}

and this is the database I created

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;
    }
}

and this is the 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" />

0

1 Answer 1

1

Your list items is emtpy when you create your ArrayAdapter. You need to fill the list with all the items already inserted into database.

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);

Next you need to change your DataBaseOwn.get(). The get()-Method should return only items existing in the database.

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;
}

UPDATE:

db.insert(item) is never called in your activity. so the items are only added to the listview, but not inserted into your database. delete the line books = db.get(); from your onClick() and add this line dbo.insert(item.getText().toString() to your addItemList()

Sign up to request clarification or add additional context in comments.

9 Comments

I made the changes you have suggested, but I am still not getting the values from the database.
Okay, I see that it was never called and now Insert is being called. Unfortunately, now I am getting a null pointer error and I can't see why. I posted the errors in the question since it won't allow me to here.
10-17 07:09:15.274: E/AndroidRuntime(3118): FATAL EXCEPTION: main 10-17 07:09:15.274: E/AndroidRuntime(3118): java.lang.NullPointerException 10-17 07:09:15.274: E/AndroidRuntime(3118): at bookshelf.Android.Java.DataBaseOwn.Insert(DataBaseOwn.java:52) 10-17 07:09:15.274: E/AndroidRuntime(3118): at bookshelf.Android.Java.Own.addItemList(Own.java:86) 10-17 07:09:15.274: E/AndroidRuntime(3118): at android.view.View$PerformClick.run(View.java:14105) 10-17 07:09:15.274: E/AndroidRuntime(3118): at android.os.Handler.handleCallback(Handler.java:605)
10-17 07:09:15.274: E/AndroidRuntime(3118): at android.os.Handler.dispatchMessage(Handler.java:92) 10-17 07:09:15.274: E/AndroidRuntime(3118): at android.os.Looper.loop(Looper.java:137) 10-17 07:09:15.274: E/AndroidRuntime(3118): at android.app.ActivityThread.main(ActivityThread.java:4424) 10-17 07:09:15.274: E/AndroidRuntime(3118): at java.lang.reflect.Method.invokeNative(Native Method) 10-17 07:09:15.274: E/AndroidRuntime(3118): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 07:09:15.274: E/AndroidRuntime(3118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 10-17 07:09:15.274: E/AndroidRuntime(3118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 10-17 07:09:15.274: E/AndroidRuntime(3118): at dalvik.system.NativeStart.main(Native Method)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.