从具有条件的两个表中检索数据

时间:2012-07-27 09:49:14

标签: android database

在我的应用程序中,我必须使用数据库表中的条目来检查条件 所以我必须从数据库中的2个表中检索数据 从表1中我使用简单的cursoradapter在列表视图中检索和显示数据。 从表2我只需要从表中检索数据,我必须检查条件。当我尝试从2个表中检索数据时,它总是返回表中的第1个条目。请帮帮我..

我的代码如下:

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

1 个答案:

答案 0 :(得分: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(); 
}