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" />