我有点不知所措,为什么我的列表视图不会填充我的简单数据库信息。任何帮助将不胜感激。我的 SQL 语句有问题吗?我的印象是它是正确的,但我猜我的光标可能是坏的。或者也许我的简单光标适配器没有任何意义(我意识到它已被弃用,但认为它仍然可以工作)
package com.example.firstdatabase;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.database.sqlite.SQLiteDatabase;
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;
import android.content.Context;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_test);
SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS FunnyNames (Email VARCHAR, FirstName VarChar, LastName VARCHAR);");
db.execSQL("INSERT INTO FunnyNames VALUES('hello@aol.com', 'Jeff', 'Bath');");
Cursor cur = db.rawQuery("SELECT Email FROM FunnyNames", null);
cur.close();
db.close();
ListView listview = (ListView) findViewById(R.id.listView1);
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cur,
new String[] {"1"},
new int[] {R.id.textView1}
);
listview.setAdapter(sca);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的列表视图 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/listView1"
android:layout_width="188dp"
android:layout_height="307dp" >
</ListView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
谢谢你!