Snowie
Snowie

Reputation: 13

format a time textview in a Sqllite database Listview

Pigeon android noobie here. I am trying to format a database value for stored time on a DBcolumn. The code that i'm using is not formatting the data at all and just displays the formatted data If i debug the code the correct value is getting passed into the formatting piexe of cosde but it is not getting passed into the UI textview as formatted I must be close :)

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

Looking forward to your replies

Snowie

Upvotes: 0

Views: 120

Answers (1)

Snowie
Snowie

Reputation: 13

Sort It. This helped alot Changing values from Cursor using SimpleCursorAdapter

Final code

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

    }

There is also a sqlLite syntax comment in the above link that would do the same thing when the Data Adaptor is querying the database

Upvotes: 1

Related Questions