samedi 3 octobre 2015

android list view join two tables by date

Hello I am new to android programing and I have problem with SQLite db.

I manage to get some information from one of my tables, and to populate my list view, that's ok but my problem occurs when I try to join two tables by date column, fore instance if I have just one row in both my tables everything works just fine, but if I insert one more line in both tables I get 6 results in my list view and if i insert third line in both tables I get 9 lines in my list view, but all results will be mixed, he combine first line from one table with some other line in other table and this is my problem!!!

I try to find a solution to this situation for 3 and a half days now and it is really frustrating!!!

PLEASE HELP ME TO SOLVE MY PROBLEM!

so this is my method that returns line from db and populate cursor:

  public Cursor returnCursorTest(){
    SQLiteDatabase db = helper.getWritableDatabase();

    String query = "SELECT * FROM table_1 inner join table_2 WHERE table_1.Date = table_2.Date";
    Cursor cursor = db.rawQuery(query, null);

    return cursor;
}

This is class that I used to populate my listView:

public class MyAdapter extends CursorAdapter {
public MyAdapter(Context context, Cursor c) {
    super(context, c);
}

// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_view_custom_item_temp,parent,false);
}


// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {

    // Find fields to populate in inflated template
    TextView information_1 = (TextView) view.findViewById(R.id.textViewInformation_1);                  
    TextView information_2 = (TextView) view.findViewById(R.id.textViewInformation_2);
    TextView information_3 = (TextView) view.findViewById(R.id.textViewInformation_3);

    TextView information_4 = (TextView) view.findViewById(R.id.textViewInformation_4);
    TextView information_5 = (TextView) view.findViewById(R.id.textViewInformation_5);
    TextView information_6 = (TextView) view.findViewById(R.id.textViewInformation_6);




    // Extract properties from cursor and populate fields with extracted properties

String info1 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.Helper.COLUMN_1));
    information_1.setText(info1);

double info2 = cursor.getDouble(cursor.getColumnIndexOrThrow(DatabaseAdapter.Helper.COLUMN_2));
    information_2.setText(String.valueOf(info2));

    String info3 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.Helper.COLUMN_3));
    information_3.setText(info3);

    String info4 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.Helper.COLUMN_OTHER_TABLE_1));
    information_4.setText(info4);

    String info5 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.Helper.COLUMN_OTHER_TABLE_2)); 
    information_5.setText(info5);

    String info6 = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseAdapter.Helper.COLUMN_OTHER_TABLE_3)); 
    information_6.setText(info6);
}

}

My tables looks like this:

table_1:

_id | column_1 | column_2 | column_3 | Date

table_2:
_id | column_other_table_1 | column_other_table_2 | column_other_table_3 | Date

THANKS to anyone who can help me!!!

Aucun commentaire:

Enregistrer un commentaire