我需要将ListView的数据存储到SQLite数据库中。当我单击列表中的名称时,它应该存储到数据库中。我怎么做?我需要创建DBAdapter类吗? 请帮忙。
答案 0 :(得分:-1)
以下是DBAdapter,它包含插入和其他有用的方法。我给你数据库结构。您可以在列表中放置一个监听器。尝试google搜索listview onClick监听器。
import java.util.ArrayList;
import com.Test7.Tasks;
import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DBAdapter {
private Context context;
private SQLiteDatabase notesdb;
private DBHelper helper;
public DBAdapter(Context context)
{
this.context = context;
helper = new DBHelper(context);
}
public void open()
{
notesdb = helper.getWritableDatabase();
}
public void close()
{
notesdb.close();
}
public long insertTasks(Tasks tasks)
{
ContentValues values = new ContentValues();
values.put(DBHelper.TASKS_TITLE, tasks.getTasksTitleString());
return notesdb.insert(DBHelper.TABLE_NAME, null, values);
}
public long updateTask(Tasks tasks, int id)
{
ContentValues values = new ContentValues();
values.put(DBHelper.TASKS_TITLE, tasks.getTasksTitleString());
return notesdb.update(DBHelper.TABLE_NAME, values, DBHelper.TASKS_ID + "= ? ",
new String[] { String.valueOf(id) });
}
public ArrayList<Tasks> getAllTasks()
{
ArrayList<Tasks> tasks = new ArrayList<Tasks>();
String[] columns = {DBHelper.TASKS_ID, DBHelper.TASKS_TITLE};
Cursor cursor = notesdb.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
String tasksTitleString = "";
int count = cursor.getCount();
int id = 0;
if(cursor != null && count > 0)
{
cursor.moveToFirst();
for (int i = 0; i < count; i++) {
id = cursor.getInt(cursor.getColumnIndex(DBHelper.TASKS_ID));
tasksTitleString = cursor.getString(cursor.getColumnIndex(DBHelper.TASKS_TITLE));
cursor.moveToNext();
}
}
return tasks;
}
public int getID(Tasks tasks)
{
String[] columns = {DBHelper.TASKS_ID, DBHelper.TASKS_TITLE};
Cursor cursor = notesdb.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
String tasksTitleString = "";
int count = cursor.getCount();
int id = 0;
if(cursor != null && count > 0)
{
cursor.moveToFirst();
for (int i = 0; i < count; i++) {
id = cursor.getInt(cursor.getColumnIndex(DBHelper.TASKS_ID));
tasksTitleString = cursor.getString(cursor.getColumnIndex(DBHelper.TASKS_TITLE));
if(tasksTitleString.equals(tasks.getTasksTitleString()))
{
return id;
}
cursor.moveToNext();
}
}
return id;
}
}
使用以下类构建数据库。尝试为您的数据库创建POJO。我跟随。
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "tasksdb8";
public static final String TABLE_NAME = "tasks";
public static final String TASKS_ID = "_id";
public static final String TASKS_TITLE = "title";
private static String CREATE_TABLE_SQL = "Create table "
+ DBHelper.TABLE_NAME + " ( " + DBHelper.TASKS_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + DBHelper.TASKS_TITLE
+ " TEXT);";
public DBHelper(Context context)
{
super(context, DBHelper.DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DBHelper.CREATE_TABLE_SQL);
Log.d("DBHelper", "Table Created: " + DBHelper.CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
这是我的POJO:
public class Tasks {
private String tasksTitleString;
private int id;
public Tasks(String tasksTitleString, int id) {
super();
this.tasksTitleString = tasksTitleString;
this.id = id;
}
public Tasks(String tasksTitleString) {
super();
this.tasksTitleString = tasksTitleString;
}
public String getTasksTitleString() {
return tasksTitleString;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String isTasksCompleted() {
return tasksCompleted;
}
public void setTasksCompleted(String tasksCompleted) {
this.tasksCompleted = tasksCompleted;
}
}