2014-01-29 24 views
1

朋友你好不工作我wnat明智的在我的應用程序了顯示數據,每月我的代碼如下的strftime(( '%Y-%M',tr_date)SQLite中的Android

public class IncomeActivity extends ListActivity { 

Button mbutoonmonth; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_income); 
    DatabaseConnectionAPI mDatabaseConnectionAPI=new DatabaseConnectionAPI(getApplicationContext()); 
mTextViewMonth.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      String StartDate=""; 


      int mYear = 0; 
      int mMonth; 
      int mDay; 
      int dayOFWeek = 0; 
      String mondayDate=""; 
      Calendar mCalendar = Calendar.getInstance(); 
      mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); 

      mCalendar.add(Calendar.DATE, 0); 

      mYear = mCalendar.get(Calendar.YEAR); 
      mMonth = mCalendar.get(Calendar.MONTH) + 1; 
      mDay = mCalendar.get(Calendar.DAY_OF_MONTH); 

      if (mMonth==10|mMonth==11|mMonth==12) { 
       StartDate=String.valueOf(mYear)+"-"+String.valueOf(mMonth); 
       System.out.println("Start DAte "+StartDate); 
      } 
      else { 
       StartDate=String.valueOf(mYear)+"-0"+String.valueOf(mMonth); 
       System.out.println("Start DAte "+StartDate); 
      } 

      mArrayListTransactions=mDatabaseConnectionAPI.getIncomeMonthData(mStringTrMode,StartDate); 
      mCustomListviewAdapter=new CustomListviewAdapter(IncomeActivity.this, mArrayListTransactions); 
      mListView.setAdapter(mCustomListviewAdapter); 
     } 
    }); 
    } 
} 

DatabaseConnectionAPI.java

public ArrayList<ParsesrTransaction> getIncomeMonthData(String mode ,String date) { 

    ArrayList<ParsesrTransaction> mGetCategoryList = new ArrayList<ParsesrTransaction>(); 
    try { 
     String sqlQuery = "select tr_date from Transaction_Master where strftime('%Y-%m', tr_date) = "+ "'"+date+"'" ; 
     Cursor mCursorCategory = Query(sqlQuery); 
     if (mCursorCategory != null) { 
      mCursorCategory.moveToFirst(); 
      while (!mCursorCategory.isAfterLast()) { 

       ParsesrTransaction mParserCategory = new ParsesrTransaction(); 
       mParserCategory 
       .setCatID(mCursorCategory.getString(mCursorCategory 
         .getColumnIndex("cat_id"))); 
       mParserCategory 
       .setCatName(mCursorCategory.getString(mCursorCategory 
         .getColumnIndex("cat_name"))); 
       mParserCategory 
       .setTrDate(mCursorCategory.getString(mCursorCategory 
         .getColumnIndex("tr_date"))); 
       mParserCategory 
       .setTrDetail(mCursorCategory.getString(mCursorCategory 
         .getColumnIndex("tr_detail"))); 
       mParserCategory 
       .setTrMode(mCursorCategory.getString(mCursorCategory 
         .getColumnIndex("tr_mode"))); 
       mParserCategory 
       .setTrPrice(mCursorCategory.getString(mCursorCategory 
         .getColumnIndex("tr_price"))); 
       mGetCategoryList.add(mParserCategory); 
       mCursorCategory.moveToNext(); 
      } 
     } 
     mCursorCategory.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return mGetCategoryList; 
} 

當我運行abouve代碼,它給我的消息像

01-29 14:27:11.890: E/CursorWindow(9965): Bad request for field slot 0,-1. numRows = 3, numColumns = 1 
01-29 14:27:11.890: W/System.err(9965): java.lang.IllegalStateException: get field slot from row 0 col -1 failed 
01-29 14:27:11.890: W/System.err(9965):  at android.database.CursorWindow.getString_native(Native Method) 
01-29 14:27:11.900: W/System.err(9965):  at android.database.CursorWindow.getString(CursorWindow.java:329) 
01-29 14:27:11.900: W/System.err(9965):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 
01-29 14:27:11.900: W/System.err(9965):  at pkg.android.rootways.rootmoney.helper.DatabaseConnectionAPI.getIncomeMonthData(DatabaseConnectionAPI.java:646) 
01-29 14:27:11.900: W/System.err(9965):  at pkg.android.rootways.rootmoney.IncomeActivity$9.onClick(IncomeActivity.java:268) 

任何想法如何解決它?

+0

從Transaction_Master中選擇tr_date,其中tr_date =「+」'「+ strftime('%Y-%m',date)+」'「 –

回答

2

您只將tr_date列投影到遊標,但也嘗試訪問其他五列。對於遊標中不存在的列,getColumnIndex()返回-1。可能是而不是SELECT tr_date可以工作。

+0

:它工作! –

相關問題