2013-02-09 155 views
0

首先,我是一個完整的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); 
+0

您需要遍歷遊標並填充列表項。 – smk 2013-02-09 16:15:57

+2

使用SimpleCursorAdapter。 – Leandros 2013-02-09 16:18:27

+0

我現在嘗試這樣做[請參閱更新],但它FC。但看不出爲什麼:-( – 2013-02-09 16:28:12

回答

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適配器=新SimpleCursorAdapter(此,R.layout.row_myrow,testCursor,

新的String [] {DBHelper.DESTINATION.NAME},新的INT [] {R.id.rowText},

SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);

testList.setAdapter(adapter);