1

In my application I have to check condition using entries in database table.
So I have to retrieve data from 2 tables in the database.
From table 1 I am retrieving and showing data in a listview using a simple cursoradapter. From table 2 I have to just retrieve the data from table and I have to check the condition. When I tried to retrieve data from 2 tables it always returns 1st entry in the table. Please help me..

My code is as follows:

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

1 回答 1

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(); 
}
于 2012-07-27T09:58:44.390 回答