我正在尝试为地址簿创建一个包含名字,姓氏等的列表。在这种情况下,我想从SQLite中检索名字中的所有值并将其放入列表中。不是SQLite的专家,但努力并没有得到任何东西....得到空指针异常:/ 有人可以帮忙吗? 公共类List_fname扩展了ListActivity {
public String [] list = get_name();
public String [] get_name(){
int i = 0;
String [] lista;
lista = new String [100];
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAllContacts();
if (c.moveToFirst()) {
do {
lista[i] = c.getString(0);
i++;
} while (c.moveToNext());
}
db.close();
return lista;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ArrayList<String> list = new ArrayList<String>();
String [] listb = list;
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, listb));
}}
答案 0 :(得分:0)
要使用SQLite DB,您可以从SQLiteOpenHelper扩展。这是第一步。类创建并使用DB:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DBHelper extends SQLiteOpenHelper {
public static interface CONTACT extends BaseColumns {
String TABLE_NAME = "human";
String FIRST_NAME = "first_name";
String LAST_NAME = "last_name";
}
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
private static final String DATABASE_NAME = "someDataBase";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_TABLE_CONTACT = CREATE_TABLE + CONTACT.TABLE_NAME + " (" + CONTACT._ID
+ " INTEGER PRIMARY KEY," + CONTACT.FIRST_NAME + " TEXT, " + CONTACT.LAST_NAME + " TEXT);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_CONTACT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS " + CONTACT.TABLE_NAME);
onCreate(db);
}
public Cursor getAllContacts() {
return getReadableDatabase().query(CONTACT.TABLE_NAME, null, null, null, null, null, null);
}
public void insertContact(String firstName, String lastName) {
final ContentValues values = new ContentValues();
values.put(CONTACT.FIRST_NAME, firstName);
values.put(CONTACT.LAST_NAME, lastName);
getWritableDatabase().insert(CONTACT.TABLE_NAME, null, values);
}
}
使用ListView进行简单布局:
的xmlns:工具= “http://schemas.android.com/tools”
android:layout_width =“match_parent”
android:layout_height =“match_parent”
android:orientation =“vertical”&gt;
<ListView
android:id="@+id/testList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
使用它的活动:
import java.util.Random;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBHelper(this);
//useless part to insert some data
Random rand = new Random();
for (int i = 0; i < 20; i++) {
db.insertContact("" + i, "" + rand.nextInt(100));
}
Cursor allContacts = db.getAllContacts();
ListView testList = (ListView) findViewById(R.id.testList);
testList.setAdapter(new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, allContacts,
new String[] { DBHelper.CONTACT.FIRST_NAME, DBHelper.CONTACT.LAST_NAME }, new int[] {
android.R.id.text1, android.R.id.text2 }));
}
}