vendredi 13 février 2015

Multiple tables Data is not depicted in ListView

I am relatively new to Android, and I am forcing my way through SQL and ListViews and even though i had success with a single table and I was able to depict all the data that I wanted in the ListView etc. I have some trouble when it comes to adding a new table. (I changed the version of the database to 2, because i was getting exceptions).


I'm going to provide the code that according to my understanding is flawed from my SQLiteOpenHelper class, any insights and guidelines are highly appreciated. Bare in mind that the code in the ListView fragment used to work perfectly with the same getAllData() method while i had a single table.


Thanks a lot!!



@Override
public void onCreate(SQLiteDatabase db) {

String queryWorkout = "CREATE TABLE workouts (workoutId INTEGER PRIMARY KEY," +
"startWeight REAL, endWeight REAL, fluidDrunk REAL, fluidLoss REAL, fluidLossPerHour REAL, dehydrationLevel REAL, hydrationStatus TEXT," +
"fluidIntake TEXT)";

db.execSQL(queryWorkout);

String queryPrediction = "CREATE TABLE predictions (workoutId INTEGER PRIMARY KEY, comments TEXT, intensity TEXT, weather TEXT, clothing TEXT, duration TEXT)";


db.execSQL(queryPrediction);

}

public ArrayList<HashMap<String, String>> getAllData() {

ArrayList<HashMap<String, String>> workoutArrayList = new ArrayList<HashMap<String, String>>();

String selectQuery = "SELECT * FROM workouts INNER JOIN predictions ON workouts.workoutId = predictions.workoutId";

SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {

do {

HashMap<String, String> workoutMap = new HashMap<String, String>();


workoutMap.put("workoutId", cursor.getString(0));
workoutMap.put("comments", cursor.getString(1));
workoutMap.put("intensity", cursor.getString(2));
workoutMap.put("weather", cursor.getString(3));
workoutMap.put("clothing", cursor.getString(4));
workoutMap.put("duration", cursor.getString(5));
workoutMap.put("startWeight", cursor.getString(6));
workoutMap.put("endWeight", cursor.getString(7));
workoutMap.put("fluidDrunk", cursor.getString(8));
workoutMap.put("fluidLoss", cursor.getString(9));
workoutMap.put("fluidLossPerHour", cursor.getString(10));
workoutMap.put("dehydrationLevel", cursor.getString(11));
workoutMap.put("hydrationStatus", cursor.getString(12));
workoutMap.put("fluidIntake", cursor.getString(13));

workoutArrayList.add(workoutMap);

} while (cursor.moveToNext());

}

return workoutArrayList;

}


ListView fragment



@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

dbTools = new DBTools(getActivity());

ArrayList<HashMap<String, String>> workoutList = dbTools.getAllData();

if (workoutList.size() != 0) {

ListView listView = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

.....
}
});

ListAdapter adapter = new SimpleAdapter(
getActivity(), workoutList, R.layout.database_entry,
new String[]{"workoutId", "comments", "intensity"},
new int[]{R.id.workoutId, R.id.comments, R.id.intensity});

setListAdapter(adapter);

}
}


EDIT My two methods that add data



public void insertWorkoutData(HashMap<String, String> queryValues) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();


values.put("workoutId", queryValues.get("workoutId"));
values.put("startWeight", queryValues.get("startWeight"));
values.put("endWeight", queryValues.get("endWeight"));
values.put("fluidDrunk", queryValues.get("fluidDrunk"));
values.put("fluidLoss", queryValues.get("fluidLoss"));
values.put("fluidLossPerHour", queryValues.get("fluidLossPerHour"));
values.put("dehydrationLevel", queryValues.get("dehydrationLevel"));
values.put("hydrationStatus", queryValues.get("hydrationStatus"));
values.put("fluidIntake", queryValues.get("fluidIntake"));

db.insert("workouts", null, values);

db.close();
}

public void insertPredictionData(HashMap<String, String> queryValues) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();


values.put("workoutId", queryValues.get("workoutId"));
values.put("comments", queryValues.get("comments"));
values.put("intensity", queryValues.get("intensity"));
values.put("weather", queryValues.get("weather"));
values.put("clothing", queryValues.get("clothing"));
values.put("duration", queryValues.get("duration"));


db.insert("predictions", null, values);

db.close();
}

Aucun commentaire:

Enregistrer un commentaire