我在這裏有點損失,爲什麼我的列表視圖不會填充我簡單的數據庫信息。任何幫助將不勝感激。我的SQL語句有什麼問題嗎? 我的印象是這是正確的,但我想我的光標可能是壞的。或者,也許我simplecursoradapter沒有任何意義(我意識到這是過時,但認爲它仍然可以工作)SimpleCursorAdapter的問題和導入到Listview
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('[email protected]', '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;
}
}
這裏是我的ListView 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>
謝謝!