對不起,如果這被證明是愚蠢的。我試圖從一個具有3個EditText字段,標題,描述和例程的活動中獲取用戶的一些數據,並將其放入SQLite數據庫(並且是的,我使用的是DBAdapter),然後將其顯示在另一個活動列表中視圖。問題是,在我填寫字段並按下保存按鈕調用onClick_AddTask函數,然後返回到列表視圖活動後,沒有明顯的錯誤,但列表視圖爲空。我確實在logcat中出現了一些錯誤,但我無法真正理解它。任何形式的洞察力或建設性的批評總是受歡迎的,謝謝。從SQLite數據庫到其他活動的Android Studio ListView
將對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));
}
});
}
}
你的logcat是不相關的。這只是系統日誌。 –
把代碼放在問題中,避免將它們鏈接出來。如果鏈接中斷,這個問題對任何人都沒有什麼幫助。 – petey