首先,我是一个完整的Java菜鸟 . 到目前为止,脚本语言完成了我需要的所有工作,但现在我需要创建一个Android应用程序并迷路了 .
我正在尝试使用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代码,用固定的数组填充该列表,但无法在那里获得db查询:
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);
1 回答
使用数据库公共类DBHelper的Helper示例扩展了SQLiteOpenHelper {
并像这里创建适配器:
DBHelper dataHelper = new DBHelper(this);
ListView testList =(ListView)findViewById(R.id.mylist);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row_myrow,testCursor,
new String [] ,new int [] ,
SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);
testList.setAdapter(适配器);