首先,我是一个完整的Java noob。到目前为止,脚本语言完成了我需要的所有工作,但现在我需要创建一个Android应用程序并迷路了。从sqlite填充listview
我想用sqlite数据库中的项目填充ListView。这项工作的各个部分,但我不能让他们一起工作。
数据库查询:
Cursor cursor = database.query("db", new String[] { "_id", "name", "count", "should" }, null, null, null, null, null);
listitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/ItemName"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ItemCount"
android:text="TextView" />
<TextView
android:id="@+id/ItemCount"
android:layout_width="50dp"
android:layout_toLeftOf = "@+id/buttonminus"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:text="TextView" />
<TextView
android:id="@+id/ItemShould"
android:layout_width="50dp"
android:layout_toLeftOf = "@+id/buttonminus"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:text="TextView" />
<Button
android:id="@+id/buttonminus"
android:layout_width="40dp"
android:layout_height="40dp"
android:onClick="buttonMinus"
android:layout_centerVertical="true"
android:layout_toLeftOf = "@+id/buttonplus"
android:focusable="false"
android:text="-" />
<Button
android:id="@+id/buttonplus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight = "true"
android:focusable="false"
android:layout_centerVertical="true"
android:onClick="buttonPlus"
android:text="+" />
</RelativeLayout>
和我有填充该列表具有固定阵列,但不能管理,以获得数据库查询工作的Java代码在那里:
private String[][] ItemsArray =
{{"Test1","10"},
{"Test2","20"},
{"Test3","123"},
{"Test4","456"}};
HashMap<String,String> item;
for(int i=0;i<ItemsArray.length;i++){
item = new HashMap<String,String>();
item.put("ItemName", ItemsArray[i][0]);
item.put("ItemCount", ItemsArray[i][1]);
item.put("ItemShould", ItemsArray[i][2]);
list.add(item);
}
sa = new SimpleAdapter(this, list, R.layout.mylistitem, new String[] { "ItemName","ItemCount","ItemShould" }, new int[] {R.id.ItemName, R.id.ItemCount, R.id.ItemShould});
setListAdapter(sa);
另外我需要把id放在那个列表的某个地方(不可见),以便以后使用它删除该行,不知道该怎么做:-(
帮助真的很感激。
更新:试过这样的,但是这会导致应用FC
Cursor cursor = database.query("inventory", new String[] { "_id", "name", "ist", "soll" }, null, null, null, null, null);
int i = 0;
cursor.moveToFirst();
HashMap<String,String> item;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
while (cursor.isAfterLast() == false) {
item = new HashMap<String,String>();
item.put("ItemName", cursor.getString(1));
item.put("ItemCount", cursor.getString(2));
list.add(item);
cursor.moveToNext();
}
cursor.close();
sa = new SimpleAdapter(this, list, R.layout.mylistitem, new String[] { "ItemName","ItemCount","ItemShould" }, new int[] {R.id.ItemName, R.id.ItemCount, R.id.ItemShould});
setListAdapter(sa);
您需要遍历游标并填充列表项。 – smk 2013-02-09 16:15:57
使用SimpleCursorAdapter。 – Leandros 2013-02-09 16:18:27
我现在尝试这样做[请参阅更新],但它FC。但看不出为什么:-( – 2013-02-09 16:28:12