2014-04-04 26 views
1

我目前有一個應用程序,它包含一個從sqlite數據庫填充的listView。 ListView包含每個項目的兩個文本視圖,一個用於名稱,另一個用於狀態。狀態顯示爲0或1,如真或假。我想讓每個包含數字0或1的項目作爲顯示打開或關閉的狀態。Android:在ListView中讀取整數作爲字符串

方法爲ListView:

// Return all data in the database. 
    public Cursor getAllRows() { 
     String where = null; 
     Cursor c = ourDatabase.query(true, DATABASE_TABLE, ALL_KEYS, where, 
       null, null, null, null, null); 
     if (c != null) { 
      c.moveToFirst(); 
     } 
     return c; 
    } 

    // also added 
    // Get a specific row (by rowId) 
    public Cursor getRow(long rowId) { 
     String where = KEY_ROWID + "=" + rowId; 
     Cursor c = ourDatabase.query(true, DATABASE_TABLE, ALL_KEYS, where, 
       null, null, null, null, null); 
     if (c != null) { 
      c.moveToFirst(); 
     } 
     return c; 
    } 

這裏是我的ListView代碼:

private void loadListView() { 
    // TODO Auto-generated method stub 
    Locker getList = new Locker(this); 
    getList.open(); 
    Cursor cursor = getList.getAllRows(); 
    startManagingCursor(cursor); 
    // setup mapping 
    String[] fromFieldNames = new String[] { getList.KEY_LOCKERNUMBER, 
      getList.KEY_STATUS }; 

    int[] toViewIDs = new int[] { R.id.tvLockerNumber, R.id.tvSatus }; 

    // create adapter 
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, 
      R.layout.itemlayout, cursor, fromFieldNames, toViewIDs); 

    // set the adapter 
    ListView myList = (ListView) findViewById(R.id.lvInfo); 
    myList.setAdapter(myCursorAdapter); 
} 

我怎麼能去這樣做?

+0

「我想讓每個包含數字0或1的項目顯示爲打開或關閉狀態。」你打開或關閉的意思是什麼?你在談論不同的佈局嗎? – FMontano

+0

目前在列表視圖中,它從我的數據庫中獲取狀態變量,它可以是0或1.我希望我的列表視圖解釋這些整數並在textview上顯示爲打開或關閉。 – JohnnyWineShirt

回答

1

你將不得不實行myCursorAdapter的方法setViewValue,您可以設置TextView的打開或關閉時,1或0

看看到this answer一個exmample。另外,請查看developer's guide瞭解更多詳情。