2012-07-27 141 views
1

在我的應用程序中,我必須使用數據庫表中的條目檢查條件。
所以我必須從數據庫中的2個表中檢索數據。
從表1我使用一個簡單的cursoradapter檢索和顯示列表視圖中的數據。 從表2我必須檢索表中的數據,我必須檢查條件。當我試圖從2個表中檢索數據時,它總是返回表中的第一個條目。請幫我..從條件中檢索兩個表中的數據

我的代碼如下:

Cursor c = db.getExpensetitle(intent.getStringExtra("grpsdbexp"));------->This is from table2 to show in listview. 
      startManagingCursor(c);  

      from = new String[] {db.KEY_DATE,db.KEY_DESC,db.KEY_INCOME,db.KEY_QUANTITY,db.KEY_ROWID}; 
      to = new int[] {R.id.text1 ,R.id.text3,R.id.text5,R.id.text7,R.id.text11}; 

        SimpleCursorAdapter notes = 
          new SimpleCursorAdapter(this, R.layout.columnviewexp, c, from, to) 
{ 
    @Override 
    public void bindView(View view, Context context, Cursor cursor) 
    { 
     String reurrence= cursor.getString(cursor.getColumnIndex("recurrence")); 
     float total=Float.valueOf(cursor.getString(cursor.getColumnIndex("total"))); 
     TextView text1=(TextView)view.findViewById(R.id.text1); 
     TextView text3=(TextView)view.findViewById(R.id.text3); 
     TextView text5=(TextView)view.findViewById(R.id.text5); 
     TextView text7=(TextView)view.findViewById(R.id.text7); 
     TextView text9=(TextView)view.findViewById(R.id.text9); 
     TextView text11=(TextView)view.findViewById(R.id.text11); 

     TextView recccind=(TextView)findViewById(R.id.reccurind);  
     String text=null;  
     String recctotal; 
     if(reurrence.equals("true")) 
     {  
      Cursor c1=db.recctable();----------------->This is from table1. 
      startManagingCursor(c1);  
      String date=c1.getString(c1.getColumnIndex("startdate"));---->This always returns 1st entry in the table. 
      String type=c1.getString(c1.getColumnIndex("recurrencetype")); 

      int recc=      Integer.parseInt(c1.getString(c1.getColumnIndex("increment")));  
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
      String currentDateandTime = sdf.format(new Date()); 
      Calendar cal=Calendar.getInstance(); 
      Date dt=null; 
      try 
      { 
       dt = sdf.parse(date); 
       cal.setTime(dt); 
      } catch (ParseException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      int daysInSameMonth=0; 
      for(int i=1;i<recc;i++) 
      { 
       int currentDay= cal.get(Calendar.DAY_OF_MONTH); 
       int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
       daysInSameMonth++; 
       if(type.equals("Daily")) 
       {   
        cal.add(Calendar.DAY_OF_MONTH, 1);     
        if(currentDay==lastDay) 
        {    
         break; 
        } 
       } 
+0

u能發佈更多一些代碼? – Archana 2012-07-27 09:55:38

+0

您使用哪些查詢?那麼情況如何? – 2012-07-27 09:59:19

+0

請看我編輯的帖子.. – 2012-07-27 10:01:30

回答

1

您需要循環光標:

List<String> dates = new ArrayList<String>(); 
List<String> types = new ArrayList<String>(); 
    while(c1.moveToNext()){ 
     dates.add(c1.getString(c1.getColumnIndex("startdate"))); 
     types.add(c1.getString(c1.getColumnIndex("recurrencetype"))); 
    } 

或者這樣:

c1.moveToFirst(); 
while (c1.isAfterLast() == false) { 
//your code to store the values here 
c1.moveToNext(); 
} 
+0

謝謝lorraine..But c1.hasNext()不支持我..我可以使用c1.Movetonext() – 2012-07-27 10:08:59

+0

嘗試使用moveToNext()代替 – lorraine 2012-07-27 10:13:35

+0

但它顯示forcetoclose錯誤: 07-27 15:42:49.520:E/AndroidRuntime(9064):android.database.CursorIndexOutOfBoundsException:請求索引2,大小爲2 – 2012-07-27 10:15:27