将用户数据保存到数据库中并在ListView中显示

时间:2015-06-06 20:48:50

标签: java android sqlite android-listview

我希望用户输入三个字段,日期,说明和时间,然后单击“提交”按钮。单击该按钮时,我希望将此数据保存到数据库中。我希望用户能够单击“上一个日志”按钮,并查看列表视图中显示的先前保存的数据。

我是一般从事数据库和android编程的新手,但是我已经使用这个网站和其他教程来编写一个我认为类似于正确的基本代码。

这是单击“提交”按钮时将调用的方法:

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

在onCreate方法的早期,我将数据库适配器设置为

final DBAdapter db = new DBAdapter(this);
    db.open();

这是我的DataBase Adapter类(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);
}

}

这是我的Previous类,当它启动时,我希望显示数据列表视图。

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


}

getter和setter方法都位于一个单独的类中,该类具有String变量time,date和description以及int id。它还有这三个构造函数:

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;

}

我的问题是,当我输入三个字段的信息时,我认为它会保存,因为上面的第一个方法被调用(我用吐司测试过),但当我尝试进入应该加载ListView的页面时,有一个错误,它崩溃了。 知道为什么会这样吗?此外,我不确定下面的代码是否完全准确,所以如果您有任何提示或修复将非常有用。谢谢!

0 个答案:

没有答案