mardi 31 mars 2015

Add and Retrieve data from SQLite Database Android in listview

Ok so i'm new here and i know this question has been asked many times but i cant seem to get anything to work i can get some samples i've found to work fine in a listview but if i try to modify or recreate i just cant seem to get it to work so im curreouse as to what i am doing wrong my code....sorry in advance for the bad formatting.



public class OrgEventCreationActivity extends Activity {

private SQLiteAdapter mySQLiteAdapter;
public EditText eventnames;
public EditText evedes;
public EditText numofv;
public EditText possisions;
public EditText skills;
public EditText minage;
public Button saveevent;
public Button proceed;
public Button stime;
public Button etime;
public Button date;
public ListView list;
public Button submit;

/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_org_event_creation);

eventnames = (EditText)findViewById(R.id.eventnameinput);
evedes = (EditText)findViewById (R.id.eventdescriptioninput);
numofv = (EditText)findViewById (R.id.numofv);
possisions = (EditText)findViewById (R.id.posinput);
skills = (EditText)findViewById (R.id.skillsinput);
minage = (EditText)findViewById (R.id.minage);
saveevent =(Button)findViewById (R.id.aeventbtn);
proceed = (Button)findViewById (R.id.submitbtn);
stime = (Button)findViewById (R.id.stimebtn);
etime = (Button)findViewById (R.id.etimebtn);
date = (Button)findViewById (R.id.datepicker);
list = (ListView)findViewById (R.id.orglist);
submit = (Button)findViewById (R.id.submitbtn);
submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent us = new Intent (OrgEventCreationActivity.this,UserHomeActivity.class);
startActivity(us);
}
});

saveevent.setOnClickListener(new View.OnClickListener() {

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

}
public void populatelist(){

String evename = eventnames.getText().toString().trim();

/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*/

mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();

mySQLiteAdapter.insert(evename);


mySQLiteAdapter.close();

Intent log = new Intent(OrgEventCreationActivity.this, UserHomeActivity.class);
startActivity(log);


}


mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();

Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);

String[] from = new String[]{SQLiteAdapter.MYDATABASE_TABLE};
int[] to = new int[]{R.id.text};

SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

listContent.setAdapter(cursorAdapter);

mySQLiteAdapter.close();


}
}
}


the activity to display listview



import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class UserHomeActivity extends Activity {

private SQLiteAdapter mySQLiteAdapter;

/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listContent = (ListView)findViewById(R.id.contentlist);

/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*
* mySQLiteAdapter = new SQLiteAdapter(this);
*mySQLiteAdapter.openToWrite();
* mySQLiteAdapter.deleteAll();

*mySQLiteAdapter.insert("Charity Dinner");
*mySQLiteAdapter.insert("Fundraiser");
*mySQLiteAdapter.insert("Benifit Concert");
*mySQLiteAdapter.insert("Silent Auction");
*

*mySQLiteAdapter.close();
*/
/*
* Open the same SQLite database
* and read all it's content.
*/
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();

Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);

String[] from = new String[]{SQLiteAdapter.MYDATABASE_TABLE};
int[] to = new int[]{R.id.text};

SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

listContent.setAdapter(cursorAdapter);

mySQLiteAdapter.close();


}
}


and this is my database



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.view.View.OnClickListener;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter {

public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String NAME_COLUMN = "EVENTS";
public static final String KEY_ID = "_id";

public static final String KEY_CONTENT = "events";

//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key aautoincrement,"+ "evnents text);";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public SQLiteAdapter(Context c){
context = c;
}



public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}

public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}

public void close(){
sqLiteHelper.close();
}

public void insertEntry(String evename)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("EVETS",evename);


// Insert the row into your table
sqLiteDatabase.insert("ev", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}





public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}

public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
NAME_COLUMN, null, null, null, null);

return cursor;
}

public class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

}


i've been trying to figure this out for days now if you can help i would be truly greatful


Aucun commentaire:

Enregistrer un commentaire