Android setListAdapter / SimpleCursorAdapter

时间:2012-02-26 06:56:28

标签: android simplecursoradapter

我正在尝试构建和Android应用程序,它似乎将Force Close保持在同一位置(setListAdapter/SimpleCursorAdapter),现在我的工作很重要。这是DBAdapterListActivity和布局(list/list row),也是Eclipse LogCat文件。我不确定是什么导致它不运行或为什么它不会:

DBAdapter:
package com.m4ltech.PasswordGenerator;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class PG_DBAdapter {
private static final String dbName = "PsdGen.db";
private static final int Database_Version = 2;
private static final String CatTbl = "CatTbl";
private static final String tempPswdTable = "tmpPswdTbl";
private static final String PswdGenTable = "PswdGenTable";
public static final String colID = "_CatID";
public static final String catName = "CatName";
    public static final String tempID = "_tempId";
public static final String tmpPswd = "tmpPswd";
public static final String PswdGen_ID = "_pswdgenID";
public static final String Comp_Site = "company_site";
public static final String UserName = "uname";
public static final String Category = "category";
public static final String Pswd = "password";
private static final String TAG = "PG_DBAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) { super(context, dbName, null, Database_Version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + CatTbl + "(" + colID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + catName
        + " text not null )");
        db.execSQL("CREATE TABLE " + tempPswdTable + "(" + tempID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + tmpPswd + " text not 
                          null )");
        db.execSQL("CREATE TABLE " + PswdGenTable + "(" + PswdGen_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " +  Comp_Site
            + " text not null, " + UserName + " text not null, "
                                    + Category + " text not null, " + Pswd + " text not null )");
        InsertCategories(db);
        InsertPswdStarter(db);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + PswdGenTable);
        db.execSQL("DROP TABLE IF EXISTS " + tempPswdTable);
        db.execSQL("DROP TABLE IF EXISTS " + CatTbl);
        onCreate(db);   }       }
public PG_DBAdapter(Context ctx) {  this.mCtx = ctx;
}
public PG_DBAdapter open() throws SQLException {mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase(); return this;
}
public void close() {    mDbHelper.close();     } 
public long createSavedPswd(String company_site, String uname,
    String category, String password) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(Comp_Site, company_site);
        initialValues.put(UserName, uname);
        initialValues.put(Category, category);
        initialValues.put(Pswd, password);
        return mDb.insert(PswdGenTable, null, initialValues);
}
public boolean deletePassword(long rowId) { return mDb.delete(PswdGenTable, PswdGen_ID 
                + "=" + rowId, null) > 0;             }
public Cursor fetchAllPasswords() {
    return mDb.query(PswdGenTable, new String[] { PswdGen_ID, Comp_Site, UserName, 
           Category, Pswd }, null, null, null, null, null, null);
}
public Cursor fetchPassword(long rowId) throws SQLException {
    Cursor mCursor = mDb.query(true, PswdGenTable, new String[] {
            PswdGen_ID, Comp_Site, UserName, Category, Pswd }, PswdGen_ID
            + " = " + rowId, null, null, null, null, null);
    if (mCursor != null) {  mCursor.moveToFirst();    }
    return mCursor;
}
public boolean updatePassword(long rowId, String uname, String category,
           String comp_site, String password) { ContentValues args = new ContentValues();
           arrgs.put(Comp_Site, comp_site); args.put(UserName, uname);  args.put(Category, 
           category);  args.put(Pswd, password);   return mDb.update(PswdGenTable, args,
           PswdGen_ID + " = " + rowId, null) > 0;
}
static void InsertCategories(SQLiteDatabase db) {
    ContentValues cv = new ContentValues();
    cv.put(colID, 1);
    cv.put(catName, "Bussiness Web");
    db.insert(CatTbl, null, cv);
    cv.put(colID, 2);
    cv.put(catName, "Bussiness");
    db.insert(CatTbl, null, cv);
    cv.put(colID, 3);
    cv.put(catName, "Personal Web");
    db.insert(CatTbl, null, cv);
    cv.put(colID, 4);
    cv.put(catName, "Personal");
    db.insert(CatTbl, null, cv);
}
static void InsertPswdStarter(SQLiteDatabase db) {
    ContentValues cv = new ContentValues();
    cv.put(PswdGen_ID, 1);
    cv.put(Comp_Site, "AOL");
    cv.put(UserName, "bsullivan");
    cv.put(Category, "Bussiness Web");
    cv.put(Pswd, "Ksj6LmKJ");
    db.insert(PswdGenTable, null, cv);
    cv.put(PswdGen_ID, 2);
    cv.put(Comp_Site, "MSN");
    cv.put(UserName, "sullivanb");
    cv.put(Category, "Personal Web");
    cv.put(Pswd, "KJKsj6Lm");
    db.insert(PswdGenTable, null, cv);
    }
   }

ListActivity 

package com.m4ltech.PasswordGenerator;
import com.m4ltech.PasswordGenerator.R;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Toast;
public class PGListActivity extends ListActivity {
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private PG_DBAdapter mDbHelper; 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    mDbHelper = new PG_DBAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}
private void fillData() {
    Cursor passwordsCursor = mDbHelper.fetchAllPasswords();
    startManagingCursor(passwordsCursor);
    String[] from = new String[] { PG_DBAdapter.Comp_Site};
    int[] to = new int[] { R.id.comp_site_row};
    SimpleCursorAdapter passwords = new SimpleCursorAdapter(this, R.layout.list_row, 
              passwordsCursor, from, to);    setListAdapter(passwords);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.main_menu, menu);
    return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_insert:
        createPassword();
        return true;
    case R.id.menu_settings:
        Intent l = new Intent(this, PGPreferences.class);
        startActivity(l);
        return true;
    }
    return super.onMenuItemSelected(featureId, item);
      }
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.mainmenuitemlngpres, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_delete:
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        mDbHelper.deletePassword(info.id);
        fillData();
        return true;        }
    return super.onContextItemSelected(item);    }
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, PGEditActivity.class);
    i.putExtra(PG_DBAdapter.PswdGen_ID, id);
    startActivityForResult(i, ACTIVITY_EDIT);       }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();    }
private void createPassword() {
    Intent i = new Intent(this, PGEditActivity.class);
    startActivityForResult(i, ACTIVITY_CREATE);    }
  }

list_row.xml

 <?xml version="1.0" encoding="utf-8"?>
 <TextView 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/comp_site_row"    android:layout_width="fill_parent" 
 android:layout_height="fill_parent"    android:padding="10dip" />

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"   android:layout_height="fill_parent"
android:layout_gravity="center"      android:background="@color/background"
android:orientation="vertical"      android:paddingTop="4dp" >

<ListView
    android:id="@+id/android:list" android:layout_width="fill_parent"  
  android:layout_height="fill_parent" />
<TextView
    android:id="@+id/android:empty"    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    android:text="@string/no_pswd_gen"
    android:textColor="#000000" />    
</LinearLayout>

logcat的:

02-25 22:59:00.515: E/AndroidRuntime(274): FATAL EXCEPTION: main
02-25 22:59:00.515: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.m4ltech.PasswordGenerator/com.m4ltech.PasswordGenerator.PGListActivity}: 
java.lang.IllegalArgumentException: column '_id' does not exist
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2663)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2679)
 02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.access$2300
(ActivityThread.java:125)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:2033)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.main
ActivityThread.java:4627)
02-25 22:59:00.515: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native
Method)
02-25 22:59:00.515: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
02-25 22:59:00.515: E/AndroidRuntime(274): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-25 22:59:00.515: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:626)
02-25 22:59:00.515: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
02-25 22:59:00.515: E/AndroidRuntime(274): Caused by: java.lang.IllegalArgumentException: 
column '_id' does not exist
02-25 22:59:00.515: E/AndroidRuntime(274): at  
android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.CursorAdapter.init
(CursorAdapter.java:111)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.CursorAdapter.<init>
(CursorAdapter.java:90)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.ResourceCursorAdapter.<init>
(ResourceCursorAdapter.java:47)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.SimpleCursorAdapter.<init>
(SimpleCursorAdapter.java:84)
02-25 22:59:00.515: E/AndroidRuntime(274): at 
com.m4ltech.PasswordGenerator.PGListActivity.fillData(PGListActivity.java:51)
02-25 22:59:00.515: E/AndroidRuntime(274): at 
com.m4ltech.PasswordGenerator.PGListActivity.onCreate(PGListActivity.java:39)
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1047)
02-25 22:59:00.515: E/AndroidRuntime(274):  at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

1 个答案:

答案 0 :(得分:1)

Cursor适配器(例如SimpleCursorAdapter)要求在提供的游标中存在_id列。

如果表中不存在_id列,则需要将其添加到表中;如果存在,则需要在SELECT语句中包含该列。

对于将来的问题,请仔细检查logcat:

ComponentInfo{com.m4ltech.PasswordGenerator/com.m4ltech.PasswordGenerator.PGListActivity}: 
java.lang.IllegalArgumentException: column '_id' does not exist