我在这个论坛上看过很多关于这个问题的帖子,但无论我多累,我都能解决这个问题。我想将数据库中的项目显示到列表视图中。我厌倦了为listview.java编写代码,从数据库中查看项目,但它没有用。
DBAdapter.java
package protect.my.password;
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;
// TO USE:
// Change the package (at top) to match your project.
// Search for "TODO", and make the appropriate changes.
public class DBAdapter {
/////////////////////////////////////////////////////////////////////
// Constants & Data
/////////////////////////////////////////////////////////////////////
// For logging:
private static final String TAG = "DBAdapter";
// DB Fields
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
/*
* CHANGE 1:
*/
// TODO: Setup your fields here:
public static final String KEY_WEBSITE = "website";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
// TODO: Setup your field numbers here (0 = KEY_ROWID, 1=...)
public static final int COL_WEBSITE = 1;
public static final int COL_USERNAME = 2;
public static final int COL_PASSWORD = 3;
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_WEBSITE, KEY_USERNAME, KEY_PASSWORD};
// DB info: it's name, and the table we are using (just one).
public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";
// Track DB version if a new version of your app changes the format.
public static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE_SQL =
"create table " + DATABASE_TABLE
+ " (" + KEY_ROWID + " integer primary key autoincrement, "
/*
* CHANGE 2:
*/
// TODO: Place your fields here!
// + KEY_{...} + " {type} not null"
// - Key is the column name you created above.
// - {type} is one of: text, integer, real, blob
// (http://www.sqlite.org/datatype3.html)
// - "not null" means it is a required field (must be given a value).
// NOTE: All must be comma separated (end of line!) Last one must have NO comma!!
+ KEY_WEBSITE + " text not null, "
+ KEY_USERNAME + " text not null, "
+ KEY_PASSWORD + " string not null"
// Rest of creation:
+ ");";
// Context of application who uses us.
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
/////////////////////////////////////////////////////////////////////
// Public methods:
/////////////////////////////////////////////////////////////////////
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 the database.
public long insertRow(String website, String username, String password) {
/*
* CHANGE 3:
*/
// TODO: Update data in the row with new fields.
// TODO: Also change the function's arguments to be what you need!
// Create row's data:
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_WEBSITE, website);
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
// Insert it 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 webSite, String userName, String passWord) {
String where = KEY_ROWID + "=" + rowId;
/*
* CHANGE 4:
*/
// TODO: Update data in the row with new fields.
// TODO: Also change the function's arguments to be what you need!
// Create row's data:
ContentValues newValues = new ContentValues();
newValues.put(KEY_WEBSITE, webSite);
newValues.put(KEY_USERNAME, userName);
newValues.put(KEY_PASSWORD, passWord);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
/////////////////////////////////////////////////////////////////////
// Private Helper Classes:
/////////////////////////////////////////////////////////////////////
/**
* Private class which handles database creation and upgrading.
* Used to handle low-level database access.
*/
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);
}
}
}
Listview.java
package protect.my.password;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Listview extends Activity {
ListView listview;
TextView empty;
String selected;
List<String> list;
String[] filenames;
DBAdapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.dblist);
openDB();
listviewitems();
myDb.close();
}
private void listviewitems() {
// TODO Auto-generated method stub
Cursor cursor = myDb.getAllRows();
// populate the message from the cursor
// Reset cursor to start, checking to see if there's data:
// Process the data:
if (cursor.moveToFirst()) {
do {
String website = cursor.getString(DBAdapter.COL_WEBSITE);
String username = cursor.getString(DBAdapter.COL_USERNAME);
String password = cursor.getString(DBAdapter.COL_PASSWORD);
String[] values = new String[] { website, username, password };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.main, R.id.dblist, values);
listview.setAdapter(adapter);
// Close the cursor to avoid a resource leak.
cursor.close();
} while(cursor.moveToNext());
}
}
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/dblist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
可能代码看起来很有趣,我不是专业但初学者。欢迎任何帮助。任何人都可以告诉我这是什么错误,或者我的代码对于Listview.java都是愚蠢的,需要重新开始。
更新1:根据我在下面得到的答案编辑代码后,它就可以了。但我有另一个问题。如果只有数据库有一些项目,那么只有应用程序可以工作,但如果数据库为空,那么应用程序崩溃。任何帮助我该如何解决?下面是我的日志猫。
08-02 19:36:36.026: W/dalvikvm(3825): threadid=1: thread exiting with uncaught exception (group=0xb4a71ba8)
08-02 19:36:36.086: E/AndroidRuntime(3825): FATAL EXCEPTION: main
08-02 19:36:36.086: E/AndroidRuntime(3825): Process: protect.my.password, PID: 3825
08-02 19:36:36.086: E/AndroidRuntime(3825): java.lang.RuntimeException: Unable to start activity ComponentInfo{protect.my.password/protect.my.password.Listview}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.os.Looper.loop(Looper.java:136)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-02 19:36:36.086: E/AndroidRuntime(3825): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 19:36:36.086: E/AndroidRuntime(3825): at java.lang.reflect.Method.invoke(Method.java:515)
08-02 19:36:36.086: E/AndroidRuntime(3825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-02 19:36:36.086: E/AndroidRuntime(3825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-02 19:36:36.086: E/AndroidRuntime(3825): at dalvik.system.NativeStart.main(Native Method)
08-02 19:36:36.086: E/AndroidRuntime(3825): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-02 19:36:36.086: E/AndroidRuntime(3825): at protect.my.password.Listview.listviewitems(Listview.java:54)
08-02 19:36:36.086: E/AndroidRuntime(3825): at protect.my.password.Listview.onCreate(Listview.java:36)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.Activity.performCreate(Activity.java:5231)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-02 19:36:36.086: E/AndroidRuntime(3825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
更新2: 得到了错误的答案。我不得不使用:
if(cursor != null && cursor.moveToFirst()){
// my codes
}
cursor.close();
答案 0 :(得分:1)
首先,您应该通知您的适配器,ListView
有其他内容,您可以使用
yourAdapter.notifyDataSetChanged();
listView.setAdapter(yourAdapter);
其次,您应该在另一个ListView
或Thread
中执行AsyncTask
的人口统计,以便数据库操作时间过长,如果您不这样做这样,在您调用ListView
填充方法后,您的数据库内容就会可用,并且您将始终为空ListView
但内容来自数据库。
答案 1 :(得分:0)
这是错的!
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.main, R.id.dblist, values);
应替换为
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
/* your row layout not your main activity layout */,
/* ID of a TextView for printing on it */, values);
或者尝试Android的简单ListView项目:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
更新#1
List<String> values = new ArrayList<String>();
do {
String website = cursor.getString(DBAdapter.COL_WEBSITE);
String username = cursor.getString(DBAdapter.COL_USERNAME);
String password = cursor.getString(DBAdapter.COL_PASSWORD);
values.add(website + " - " + username + " - " + password);
} while(cursor.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.main, R.id.dblist, values);
listview.setAdapter(adapter);
// Close the cursor to avoid a resource leak.
cursor.close();