0

Pigeon android noobie 在这里。我正在尝试格式化数据库值以在 DBcolumn 上存储时间。我正在使用的代码根本没有格式化数据,只是显示格式化的数据必须接近:)

public void PopulateListViewFromDatabase() {


        Cursor cursor = myDb.getAllRows();

        startManagingCursor(cursor);
        // Setup mapping from the cursor to view fields


        String[] fromFieldNames = new String[] {
                DBAdapter.KEY_START,
                DBAdapter.KEY_FINISH, 
                DBAdapter.KEY_ACTIONHRCOUNT,
                DBAdapter.KEY_BANGCOUNT,
                DBAdapter.KEY_RANDOMOPT
                };


        int[] to=new int [] {
                R.id.txtStartTime, 
                R.id.txtFinishTime,
                R.id.txtActionCount,
                R.id.txtBangCount,
                R.id.checkRandom
                };

    //Create adaptor to map columns of the DB onto elements in the UI.
    SimpleCursorAdapter myCursorAdapter =
            new SimpleCursorAdapter(
                    this,                       // Context
                    R.layout.frm_item,  // Row layout template
                    cursor,                     //cursor record infomation
                    fromFieldNames,             //DB column names where the infomation is coming from
                    to                  // and where the infomation is being sent to
                    );




        myCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
          public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            int nCheckedIndex = cursor.getColumnIndexOrThrow(DBAdapter.KEY_START);
            if (columnIndex == nCheckedIndex) {
                long DBStart = cursor.getLong(DBAdapter.COL_START);  //Value is being passed correctly into here!!!!
                TextView txtStart= (TextView) view;
                txtStart.setText(DateFormat.format("h:mm a",DBStart ));  // Not being sent as formatted data correctly here?????????????
            }
            return false;
          }
        });

    ListView myList = (ListView) findViewById(R.id.listviewactions);

    myList.setAdapter(myCursorAdapter);
    }

期待您的回复

斯诺伊

4

1 回答 1

1

解决。这有助于 使用 SimpleCursorAdapter 从 Cursor 更改值

最终代码

    public void PopulateListViewFromDatabase() {


        Cursor cursor = myDb.getAllRows();

        startManagingCursor(cursor);
        // Setup mapping from the cursor to view fields


        String[] fromFieldNames = new String[] {
                DBAdapter.KEY_START,
                DBAdapter.KEY_FINISH, 
                DBAdapter.KEY_ACTIONHRCOUNT,
                DBAdapter.KEY_BANGCOUNT,
                DBAdapter.KEY_RANDOMOPT
                };


        int[] to=new int [] {
                R.id.txtStartTime, 
                R.id.txtFinishTime,
                R.id.txtActionCount,
                R.id.txtBangCount,
                R.id.checkRandom
                };

    //Create adaptor to map columns of the DB onto elements in the UI.
    SimpleCursorAdapter myCursorAdapter =
            new SimpleCursorAdapter(
                    this,                       // Context
                    R.layout.frm_item,  // Row layout template
                    cursor,                     //cursor record infomation
                    fromFieldNames,             //DB column names where the infomation is coming from
                    to                  // and where the infomation is being sent to
                    );


        myCursorAdapter.setViewBinder(new ViewBinder() {
                public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

                    if (aColumnIndex == 1) {
                        long createDate = aCursor.getLong(aColumnIndex);
                        TextView textView = (TextView) aView;
                        textView.setText(DateFormat.format("h:mm a", createDate));
                        return true;
                    }
                    if (aColumnIndex == 2) {
                        long createDate = aCursor.getLong(aColumnIndex);
                        TextView textView = (TextView) aView;
                        textView.setText(DateFormat.format("h:mm a", createDate));
                        return true;
                    }
         return false;
            }
        });
    ListView myList = (ListView) findViewById(R.id.listviewactions);
    myList.setAdapter(myCursorAdapter);

    }

上面的链接中还有一个 sqlLite 语法注释,当数据适配器查询数据库时,它会做同样的事情

于 2013-07-06T22:27:51.437 回答