Android:Listview不填充从数据库获取的数据

时间:2013-03-03 16:28:33

标签: android sqlite listview simplecursoradapter

我试图通过使用游标从SQLite获取数据来填充列表视图。但每当应用程序突然关闭时显示“不幸停止”

这是一个简单的代码:

public class MainActivity extends ListActivity {

//Android listview object
ListView listViewPhoneBook;

/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listContent = (ListView) findViewById(R.id.listPhoneBook);
    DbAdapter mDb = new DbAdapter(this);
    mDb.open();

    Cursor cursor =mDb.fetchAllPrograms();
    startManagingCursor(cursor);

    String[] from = new String[] { DbAdapter.KEY_TITLE };
    int[] to = new int[] { R.id.textViewName };

    SimpleCursorAdapter cursorAdapter = new 
SimpleCursorAdapter(this, R.layout.row,  cursor, from, to);

    listContent.setAdapter(cursorAdapter);



    //Onclick ListView setlistener
    listContent.setTextFilterEnabled(true);

    listContent.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


        }

    });
    }
}

XML文件:Activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
<ListView
    android:id="@+id/listPhoneBook"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:dividerHeight="0.1dp"
    android:divider="#0000CC"
    >
    </ListView>

</LinearLayout>

Row.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
    android:id="@+id/textViewName"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:textColor="#0000FF"
    android:text="TextView" />

</LinearLayout>

数据库文件:DbAdapter.java

 public class DbAdapter{


public final static String KEY_TITLE = "title";
    public static final String KEY_BODY = "body";
    public static final String KEY_ROWID = "_id";

    private static final String TAG = "LogoDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

// Database creation sql statement


   private static final String DATABASE_NAME = "Logo";
   private static final String DATABASE_TABLE = "LogoProgram";
   private static final int DATABASE_VERSION = 1;
private final Context mCtx;

private static final String TABLE_CREATE =
        "CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
        KEY_TITLE + " TEXT, " +
        KEY_BODY + " TEXT);";

@SuppressLint("SdCardPath")
private String DATA_PATH="/data/data/com.example/Logo";

    private static class DatabaseHelper extends SQLiteOpenHelper{

    // Initialize context or database to be used as context
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(TABLE_CREATE);


    }

    //In case of updated version of database is available fire the query
    @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 Logoprogram");
        onCreate(db);
    }
}


public DbAdapter(Context ctx) {
    mCtx = ctx;
}


public DbAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();

    return this;
}

public void close() {
    mDbHelper.close();
}



public long createProgram(String title, String body) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_BODY, body);

    return  mDb.insert(DATABASE_TABLE, null, initialValues);

}

public String getdata() {

    String[] columns= new String[]{ KEY_ROWID, KEY_TITLE,KEY_BODY};
    Cursor c= mDb.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result="";

    int id=c.getColumnIndex(KEY_ROWID);
    int iTitle=c.getColumnIndex(KEY_TITLE);
    int iBody=c.getColumnIndex(KEY_BODY);

    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
        result= result + c.getString(id) + " " + c.getString(iTitle) + 
" "     + c.getString(iBody) + "\n";
        //result= result + c.getString(iTitle) + "\n";

    }

    return result;
}



public Cursor fetchAllPrograms() {

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
            KEY_BODY}, null, null, null, null, null);
}


public Cursor fetchPrograms(long rowId) throws SQLException {

    Cursor mCursor =

        mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
                null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}}

游标方法有弃用警告。谁能告诉我,我哪里出错了? 提前谢谢!

日志如下:

03-03 22:42:46.695: D/AndroidRuntime(555): Shutting down VM
03-03 22:42:46.695: W/dalvikvm(555): threadid=1: thread exiting with uncaught   
exception (group=0x409961f8)
03-03 22:42:46.727: E/AndroidRuntime(555): FATAL EXCEPTION: main
03-03 22:42:46.727: E/AndroidRuntime(555): java.lang.RuntimeException: Unable to start 
activity ComponentInfo{com.android.listview/com.android.listview.MainActivity}:  
java.lang.RuntimeException: Your content must have a ListView whose id attribute is
'android.R.id.list'
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.ActivityThread.access$600(ActivityThread.java:122)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-03 22:42:46.727: E/AndroidRuntime(555):  at   
android.os.Handler.dispatchMessage(Handler.java:99)
03-03 22:42:46.727: E/AndroidRuntime(555):  at android.os.Looper.loop(Looper.java:137)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.ActivityThread.main(ActivityThread.java:4340)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
java.lang.reflect.Method.invokeNative(Native Method)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
java.lang.reflect.Method.invoke(Method.java:511)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-03 22:42:46.727: E/AndroidRuntime(555):  at dalvik.system.NativeStart.main(Native 
Method)
03-03 22:42:46.727: E/AndroidRuntime(555): Caused by: java.lang.RuntimeException: Your 
content must have a ListView whose id attribute is 'android.R.id.list'
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.ListActivity.onContentChanged(ListActivity.java:243) 
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
03-03 22:42:46.727: E/AndroidRuntime(555):  at   
android.app.Activity.setContentView(Activity.java:1835)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
com.android.listview.MainActivity.onCreate(MainActivity.java:24)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.Activity.performCreate(Activity.java:4465)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-03 22:42:46.727: E/AndroidRuntime(555):  at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-03 22:42:46.727: E/AndroidRuntime(555):  ... 11 more
03-03 22:42:49.625: I/Process(555): Sending signal. PID: 555 SIG: 9

2 个答案:

答案 0 :(得分:1)

我发现至少有两个原因导致您的代码无法正常运行。

  1. 您在ListView处使用了错误的(ListView) findViewById(R.id.textViewName); ID。它应该是:

    ListView listContent = (ListView) findViewById(R.id.listPhoneBook);
    
  2. ListView正在尝试访问已关闭的Cursor,因为您已拨打mDb.close();。您应该在mDb.close();中致电onDestroy

  3. 修改

    由于您使用的是ListActivity,因此您应该提供身份ListView的{​​{1}}。更改您的android.R.id.list

    activity_main.xml

    您的<ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:dividerHeight="0.1dp" android:divider="#0000CC" >

    MainActivity

答案 1 :(得分:0)

我在这里可以看到的一个问题是

ListView listContent = (ListView) findViewById(R.id.textViewName);

xml中的

textViewname是TextView,而不是ListView。

使用此ListView listContent = (ListView) findViewById(R.id.listPhoneBook);