我創建了一個數據庫,其中包含了我放入ListView
的數據。由於它是以編程方式創建的,因此不存在包含此ListView
的xml文件。代碼:如何實現一個數據庫填充ListView到另一個xml創建的ListView?
public class hornot extends Application{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";
public static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_HOTNESS + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public hornot(Context c) {
ourContext = c;
}
public hornot open() throws SQLException{ //ez a throws kell az SQLiteExample.java try részhez
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
這是我如何把物品放入ListView
:
ListView lv;
ArrayList<String> todoItems = new ArrayList<String>();
ArrayList<String> todoItems2 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
final hornot info = new hornot(this);
info.open();
Cursor c = info.getAllTitles();
if (c.moveToFirst())
{
do{
todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2));
}while (c.moveToNext());
}
if (todoItems.size() > 0)
{
lv.setAdapter(new ArrayAdapter<String>(sqview.this,android.R.layout.simple_list_item_1, todoItems));
}
我創建的XML,我想放編程方式創建ListView
到另一個ListView
(ID爲listplus
):
<?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">
<Button android:text="Button" android:id="@+id/b1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:id="@+id/b2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<ListView android:id="@+id/listplus" android:layout_height="wrap_content"
android:layout_width="fill_parent"><;/ListView>
</LinearLayout>
我該怎麼做?
1.爲什麼是你的'hornot'擴展'Application'嗎? 2.你在哪裏使用ListView的佈局,ID爲listplus(這是另一個活動?) – Luksprog 2012-02-26 21:19:47
@slukian 1. hornot extend應用程序是一個錯誤,它不在那裏:) 2.是的,我想在另一個活動中使用它 – 2012-02-26 21:23:57
我不知道它是否可能,但我想所有的方法在新的列表視圖中工作,就像在舊的。 – 2012-02-26 21:30:55