1

In my app i have two different tables with different rows. I trying to show the two tables on listView with the cursorAdapter. But cursorAdapter gets query only one table to show. how can i set the two tables two one cursorAdapter? here is my code:

MainActivity:

    hand = new DbHandler(getActivity());
    Cursor c = hand.getDayCursor(workName);
    adapterC = new CustomCursorAdapter(getActivity(), c, 0);
    lv.setAdapter(adapterC);

From DbHandler:

public Cursor getSettingCursor(String workName) {
        Cursor c = null;
        try {   
            open();
            c = myDb.query(TABLE_DAY, null, "workName=?", new String[]{workName}, null, null, null, null);
            c.getInt(0);
            c.getString(1);
            c.getFloat(2); 
            c.getFloat(3);
            c.getInt(4);
            c.getInt(5); 
            c.getInt(6); 
            c.getInt(7);
            c.getInt(8); 
            c.getFloat(9); 
            c.getFloat(10);
            c.getFloat(11);
            c.getInt(12);
            c.getInt(13); 
            c.getFloat(14); 
            c.getFloat(15);
            c.getFloat(16);
            c.getInt(17);
            c.getInt(18); 
            c.getFloat(19); 
            c.getFloat(20);
            c.getFloat(21);
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            close();
        }

        return c;
    }

public Cursor getDayCursor(String workName) {
        Cursor c = null;
        try {   
            open();
         c = myDb.query(TABLE_DAY, null, "workName=?", new String[]{workName}, null, null, null, null);
            c.getInt(0);
            c.getString(1);
            c.getFloat(2); 
            c.getFloat(3);
            c.getInt(4);
            c.getInt(5); 
            c.getInt(6); 
            c.getInt(7);
            c.getInt(8); 
            c.getFloat(9); 
            c.getFloat(10);
            c.getFloat(11);
            c.getInt(12);
            c.getInt(13); 
            c.getFloat(14); 
            c.getFloat(15);
            c.getFloat(16);
            c.getInt(17);
            c.getInt(18); 
            c.getFloat(19); 
            c.getFloat(20);
            c.getFloat(21);
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            close();
        }

        return c;
    }

cursorAdapter:

@Override
public void bindView(View view, Context context, Cursor cursor) {   

int dateDay = cursor.getInt(cursor.getColumnIndex(DbHandler.DATE_DAY));
int dateMonth = cursor.getInt(cursor.getColumnIndex(DbHandler.DATE_MONTH));
int dateYear = cursor.getInt(cursor.getColumnIndex(DbHandler.DATE_YEAR)); .....
}

and one more question, in the Dbhandler ,do i have to put "c.getInt(0);...c.getString(1);.."? Thank's helpers..

Matan
  • 296
  • 5
  • 24
  • Use a `MergeCursor` ? http://developer.android.com/reference/android/database/MergeCursor.html – Antoine Marques Aug 21 '14 at 08:56
  • refer this http://stackoverflow.com/questions/3235746/listview-using-two-cursoradapters – Piyush Kukadiya Aug 21 '14 at 08:57
  • I think you need to read and learn more about SQL. It's possible to create a SQL query using `SELECT` on more than one table such as `SELECT TABLE_1.column_A, TABLE_2.column_X`. You can also use the SQL `JOIN` to join multiple queries and columns of tables (although that's somewhat more complicated). The problem with the Android helper methods is they are restrictive often to only one table. There is however a method `rawQuery` which allows using a SQL query as complicated as you like. – Squonk Aug 21 '14 at 09:14
  • Thanks i understand, but i need any good example code to my problem. i can't find on the web – Matan Aug 21 '14 at 09:42

0 Answers0