简体   繁体   中英

Android: Read integer as string in a ListView

I currently have an application that contains a listView that is populated from the sqlite database. The ListView contains two texts views for each item, one for the name and one for the status. A status is displayed either by a 0 or a 1 like true or false. I would like to make each item that contains the number 0 or 1 as the status to display open or closed.

methods for the 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;
    }

Here is my listView code:

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);
}

How can I go about doing this?

You will have to implement the method setViewValue of myCursorAdapter so you can set the textview to Open or Close when 1 or 0.

Take a look to this answer for an exmample. Also, look at the developer's guide for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM