views:

53

answers:

0

I have made or copied database, but how can I get It to work together with buttons and arraylist. Ultimately I want that itemlist is persistent (when the application dies for some reason, it should be able to present previous list again). Do I have to write some new classes or just some more row-s code???

package com.example.proov;

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 class DBAdapter { public static final String KEY_ROWID = "_id"; public static final String KEY_TITLE = "title"; private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "shopping list";
private static final String DATABASE_TABLE = "items";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
    "create table titles (_id integer primary key autoincrement,"
    + "title text not null)";

private final Context context; 

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}    

//---opens the database---
public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//---insert a title into the database---
public long insertTitle(String title) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular title---
public boolean deleteTitle(long rowId) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + 
      "=" + rowId, null) > 0;
}

//---retrieves all the titles---
public Cursor getAllTitles() 
{
    return db.query(DATABASE_TABLE, new String[] {
      KEY_ROWID, 
      KEY_TITLE},
      null,
      null,
      null,
      null,
      null

         );
}

//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {
              KEY_ROWID,

              KEY_TITLE,

              }, 
              KEY_ROWID + "=" + rowId, 
              null,
              null, 
              null, 
              null, 
              null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//---updates a title---
public boolean updateTitle(long rowId, String title) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_TITLE, title);
    return db.update(DATABASE_TABLE, args, 
                     KEY_ROWID + "=" + rowId, null) > 0;
}

}

package com.example.proov;

import java.util.ArrayList;

import com.example.proov.R;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView;

public class proovin extends Activity {

private ListView LView; ArrayList ar = new ArrayList(); ArrayAdapter ad;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    ad = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, ar);




    LView = (ListView) findViewById(R.id.ListView01);
 //      Set option as Multiple Choice. So that user can able to select more the one option
         LView.setAdapter(ad);
         LView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


      Button c = (Button) findViewById(R.id.clear);  
      Button b = (Button) findViewById(R.id.add_item);
      final EditText d = (EditText) findViewById(R.id.title);
      b.setOnClickListener(new View.OnClickListener() {


 @Override
    public void onClick(View v) {
     ad.add(d.getText().toString()); 


    }
   });
     c.setOnClickListener (new View.OnClickListener() {

@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
ad.clear(); 
}

});

}

}