简体   繁体   中英

How do I store listview of data from emulator into sqlite database?

I need to store ListView of data into SQLite database. When I click on a name in the list, it should store into database. How do I do that? Do I need to create DBAdapter class? Please help.

The following is the DBAdapter, it contains the insert and other useful methods. I'm giving you the database structure. You can put a listener in your list. Try googling listview onClick listener.

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

Use the following class to structure your database. Try creating a POJO for you database. Mine follows.

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

}

}

This is my 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;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM