0

首先,我是一个完整的 Java 菜鸟。到目前为止,脚本语言完成了我需要的一切工作,但现在我需要创建一个 Android 应用程序并且迷路了。

我正在尝试使用 sqlite 数据库中的项目填充 ListView。该工作的各个部分,但我不能让它们一起工作。

数据库查询:

Cursor cursor = database.query("db", new String[] { "_id", "name", "count", "should" }, null, null, null, null, null);

列表项.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);
4

1 回答 1

0

使用数据库公共类 DBHelper 的帮助程序示例扩展 SQLiteOpenHelper {

public static interface DESTINATION extends BaseColumns {
        String TABLE_NAME = "destination";
        String NAME = "name";
    }

private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";

private static final String CREATE_TABLE_DESTINATION = CREATE_TABLE + DESTINATION.TABLE_NAME + " (" 
            + DESTINATION._ID + " INTEGER PRIMARY KEY , " + DESTINATION.NAME + " TEXT );";

private static final String DATABASE_NAME = "TravelCompany";
    private static final int DATABASE_VERSION = 2;
public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_DESTINATION);
    }

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DESTINATION.TABLE_NAME);
        onCreate(db);
    }
public void insertDestination(String destinationName) {
        ContentValues values = new ContentValues();
        values.put(DESTINATION.NAME, destinationName);
        getWritableDatabase().insert(DESTINATION.TABLE_NAME, null, values);
    }
public Cursor getAllDestination() {
        return getReadableDatabase().query(DESTINATION.TABLE_NAME, new String[] { DESTINATION.NAME }, null,
                null, null, null, null);
    }
}
Than create layout with listview. Add another layout for one row. 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" >

&lt;TextView
        android:id="@+id/rowText" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />
</LinearLayout>

并像这里一样创建适配器:

DBHelper dataHelper = new DBHelper(this);

ListView testList = (ListView) findViewById(R.id.mylist);

SimpleCursorAdapter 适配器 = new SimpleCursorAdapter(this, R.layout.row_myrow, testCursor,

新字符串 [] { DBHelper.DESTINATION.NAME },新整数 [] { R.id.rowText },

SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);

testList.setAdapter(适配器);

于 2013-04-09T07:49:50.103 回答