I want the user to enter three fields, a date, description, and time, and then click a "Submit" button. When the button is clicked, I want this data to be saved to a database. I want the user to be able to click a "Previous Logs" button and see their previous saved data displayed in a list view.
I'm new at working with databases and android programming in general, but I've used this site and other tutorials to write a base code that I think is similar to what is correct.
This is the method that will be called when the Submit button is clicked:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dateOfPractice = String.valueOf(etDate.getText().toString());
time = Integer.toString(npHours.getValue()) + ":" + Integer.toString(npMinutes.getValue());
description = String.valueOf(etDescription.getText().toString());
db.insertPractice(dateOfPractice, time, description);
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
}
Earlier in the onCreate method I set the database adapter as so
final DBAdapter db = new DBAdapter(this);
db.open();
This is my DataBase Adapter class (DBAdapter.java)
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
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_DATE = "date";
public static final String KEY_TIME = "time";
public static final String KEY_DES = "description";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "music";
private static final String DATABASE_TABLE = "practices";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table practices (_id integer primary key autoincrement, "
+ "date text not null, time text not null, "
+ "description 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()
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertPractice(String date, String time, String description)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_TIME, time);
initialValues.put(KEY_DES, description);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deletePractice(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
//---retrieves all the titles---
public Cursor getAllPractices()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_DATE,
KEY_TIME,
KEY_DES
},
null,
null,
null,
null,
null);
}
}
This is my class of Previous logs, which when it starts, I want the listview of data to be shown.
public class PreviousPractices extends Activity {
ListView practiceList;
ArrayAdapter<String> adapter;
ArrayList<String> listItems = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.previous_practices_layout);
DBAdapter db = new DBAdapter(this);
List<Practice> pList = (List<Practice>) db.getAllPractices();
for (Practice p: pList)
{
String log = "Id: "+p.getID()+" ,Date" + p.getDate() + " ,Time: " + p.getTime() + " ,Description: " + p.getDescription();
// Writing Contacts to log
Log.d("Practice: ", log);
}
db.getAllPractices();
practiceList=(ListView)findViewById(R.id.practiceList);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,listItems);
practiceList.setAdapter(adapter);
}
The getter and setter methods are all located in a separate class that has String variables time, date, and description, and an int id. It also has these three constructors:
public Practice(int id, String date, String time, String description)
{
this.id = id; this.date = date; this.time = time; this.description = description;
}
public Practice(String date, String time, String description)
{
this.date = date; this.time = time; this.description = description;
}
My issue is that when I type the information for three fields in, I think it saves because the first method above is called (I tested with a toast), but when I try to get to the page that should load the ListView, there is an error and it crashes. Any idea why this is happening? Also I'm not sure if the code below is completely accurate, so if you have any tips or fixes that would be extremely helpful. Thanks!
Aucun commentaire:
Enregistrer un commentaire