2013-02-09 117 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);