Android Studio从SQLite数据库到其他活动ListView

时间:2016-07-28 13:35:29

标签: android sqlite listview android-activity android-sqlite

很抱歉,如果这是愚蠢的。我正在尝试从一个具有3个EditText字段,标题,描述和例程的活动中获取用户的一些数据,将其放入SQLite数据库(是的,我确实使用DBAdapter),然后将其显示在另一个活动列表中视图。问题是,在我填写字段并按下调用onClick_AddTask函数的保存按钮然后返回列表视图活动后,没有明显错误,但列表视图为空。我确实在logcat中遇到了一些错误,但我真的不明白它。任何形式的见解或建设性的批评总是受欢迎的,谢谢。

DBAdapter:

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 {

private static final String TAG = "DBAdapter"; //used for logging database version changes

// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_TASK = "task";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_DATE = "date";
public static final String KEY_EXERCISES = "exercises";

public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_TASK, KEY_DESCRIPTION, KEY_DATE, KEY_EXERCISES};

// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_TASK = 1;
public static final int COL_DATE = 2;
public static final int COL_DESCRIPTION = 3;
public static final int COL_EXERCISES = 4;

// DataBase info:
public static final String DATABASE_NAME = "dbToDo";
public static final String DATABASE_TABLE = "mainToDo";
public static final int DATABASE_VERSION = 3; // The version number must be incremented each time a change to DB structure occurs.

//SQL statement to create database
private static final String DATABASE_CREATE_SQL = 
        "CREATE TABLE " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + KEY_TASK + " TEXT NOT NULL, "
        + KEY_DATE + " TEXT, "
        + KEY_DESCRIPTION + " TEXT, "
        + KEY_EXERCISES + " TEXT"
        + ");";

private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;


public DBAdapter(Context ctx) {
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}

// Add a new set of values to be inserted into the database.
public long insertRow(String task, String date, String description, String exercises) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TASK, task);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_DESCRIPTION, description);
    initialValues.put(KEY_EXERCISES, exercises);


    // Insert the data into the database.
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return db.delete(DATABASE_TABLE, where, null) != 0;
}

public void deleteAll() {
    Cursor c = getAllRows();
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
    if (c.moveToFirst()) {
        do {
            deleteRow(c.getLong((int) rowId));              
        } while (c.moveToNext());
    }
    c.close();
}

// Return all data in the database.
public Cursor getAllRows() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String task, String date) {
    String where = KEY_ROWID + "=" + rowId;
    ContentValues newValues = new ContentValues();
    newValues.put(KEY_TASK, task);
    newValues.put(KEY_DATE, date);
    // Insert it into the database.
    return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}


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

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        // Destroy old database:
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        // Recreate new database:
        onCreate(_db);
    }
}

}

主要活动:

import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

Time today = new Time(Time.getCurrentTimezone());
DBAdapter myDB;
EditText title;
Button newTask;
EditText description;
EditText routine;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    title = (EditText) findViewById(R.id.editTextTask);
    description = (EditText) findViewById(R.id.editDescriptionTask);
    routine = (EditText) findViewById(R.id.editRoutineTask);
    newTask = (Button) findViewById(R.id.buttonNew);

    newTask.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, MenuActivity.class));
        }
    });

    openDB();
    populateListView();
    listViewItemClick();
    listViewItemLongClick();

}

private void openDB() {
    myDB = new DBAdapter(this);
    myDB.open();
}

public void populateListView() {
    Cursor cursor = myDB.getAllRows();

    String[] fromFieldNames = new String[] {
            DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK,DBAdapter.KEY_DESCRIPTION
    };

    int[] toViewIDs = new int[] {
            R.id.textViewItemNumber, R.id.textViewItemTask, R.id.textViewItemDescription
    };

    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,cursor,fromFieldNames,toViewIDs,0);
    ListView myList = (ListView)findViewById(R.id.listViewTasks);
    myList.setAdapter(myCursorAdapter);
}

private void updateTask(long id) {
    Cursor cursor = myDB.getRow(id);
    if(cursor.moveToFirst()) {
        String task = title.getText().toString();
        today.setToNow();
        String date = today.format("%Y-%M-%D %H:%M:%S");
        myDB.updateRow(id, task, date);
    }

    cursor.close();
}

private void listViewItemClick() {
    ListView myList = (ListView) findViewById(R.id.listViewTasks);
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int arg2, long id) {
            updateTask(id);
            populateListView();
            displayToast(id);
        }
    });
}

public void onClick_AddTask(View v) {
    today.setToNow();
    String timeStamp = today.format("%Y-%M-%D %H:%M:%S");
    if(!TextUtils.isEmpty(title.getText().toString())) {
        myDB.insertRow(title.getText().toString(), timeStamp, description.getText().toString(), routine.getText().toString());
    }
    populateListView();
}

public void onClick_DeleteTasks(View v) {
    myDB.deleteAll();
    populateListView();
}

private void listViewItemLongClick() {
    ListView myList = (ListView) findViewById(R.id.listViewTasks);
    myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long id) {
            myDB.deleteRow(id);
            populateListView();

            return false;
        }
    });
}

private void displayToast(long id) {
    Cursor cursor = myDB.getRow(id);
    if(cursor.moveToFirst()) {
        long idDB = cursor.getLong(DBAdapter.COL_ROWID);
        String task = cursor.getString(DBAdapter.COL_TASK);
        String date = cursor.getString(DBAdapter.COL_DATE);

        String message = "ID: " + idDB + "\n" + "Task: " + task + "\n" + "Date: " + date;

        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }
    cursor.close();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    closeDB();
}

private void closeDB() {
    myDB.close();
}

}

第二项活动:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MenuActivity extends AppCompatActivity {

Button save;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    save = (Button) findViewById(R.id.buttonAddTask);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MenuActivity.this, MainActivity.class));
        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

检查insertRow返回的内容。如果返回小于1,则表示您无法插入。

答案 1 :(得分:0)

你的问题是ListView是空的。

仔细观察PopulateListView():

null

一些线索

首先,每次调用方法时,都会继续初始化myList ListView。在实例化类时执行一次。为什么?因为您可能会使用空列表视图或其他内容覆盖列表。我不知道,但这是一个线索。

其次,我没有看到新项目如何添加到ListView中。我看到你每次都设置适配器,但我没有看到添加。也许这只是我,但这是值得关注的。

现在,这里有一些更新ListView的快速代码

在OnCreate()中 - (一次性初始化)我有:

public void populateListView() {
    Cursor cursor = myDB.getAllRows();

    String[] fromFieldNames = new String[] {
            DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK,DBAdapter.KEY_DESCRIPTION
    };

    int[] toViewIDs = new int[] {
            R.id.textViewItemNumber, R.id.textViewItemTask, R.id.textViewItemDescription
    };

    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,cursor,fromFieldNames,toViewIDs,0);
    ListView myList = (ListView)findViewById(R.id.listViewTasks);
    myList.setAdapter(myCursorAdapter);
}

然后,在我要添加项目的其他代码中,我有如下代码:

listView = (ListView) findViewById(R.id.mainListView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listViewItems);
listView.setAdapter(adapter);

这可能不是答案,但可以帮助您实现目标。 祝你好运。