2013-07-06 39 views
0

Pigeon android noobie here。 我想格式化數據庫值的存儲時間在DBcolumn上。 我使用的代碼根本不格式化數據,只是顯示格式化的數據 如果我調試代碼正確的值傳遞到cosde的格式化piexe,但它不會傳遞到UI textview作爲格式化 我必須接近:)格式化Sqllite數據庫中的時間textview列表視圖

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

期待您的答覆

Snowie

回答

1

排序。這幫助了很多 Changing values from Cursor using SimpleCursorAdapter

最終代碼

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語法評論
相關問題