jeudi 5 mars 2015

How do you get the name of objects from a specific database table rather than every Table?

I've got 4 SQLite Database tables, each one storing a Name and a Status for different Checkboxes. I've got it to save both of those variables and it can reload ONE table perfectly.


However, when I try to reload the data from multiple databases (This only happens when the check boxes are checked), it overwrites all of the other databases and makes all of the names the same rather than just loading the original names.


How do I retrieve the status of each check box in each table without overwriting the other checked check boxes?


Here is the Code that should reload the data but doesn't.



public View getView(int position, View otherView, ViewGroup parent) {
CheckBox chk = null;
ImageButton btn = null;
if (otherView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
otherView = inflater.inflate(R.layout.item_layout,
parent, false);
chk = (CheckBox) otherView.findViewById(R.id.checkbox);
otherView.setTag(chk);
btn = (ImageButton) otherView.findViewById(R.id.delete);


chk.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//This does not seem to be the issue

CheckBox cb = (CheckBox) v;
ItemClass changeTask = (ItemClass) cb.getTag();
changeTask.setStatus(cb.isChecked() == true ? 1 : 0);
DataBaseHelper.getInstance(context).upDateDataBase(changeTask);
Toast.makeText(
context,
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_SHORT)
.show();
}

});

} else {

chk = (CheckBox) otherView.getTag();
}

ItemClass current = itemList.get(position);

//The issue is around here but I can't figure it out

chk.setText(current.getName());
chk.setChecked(current.getStatus() == 1 ? true : false);
chk.setTag(current);
return otherView;
}


My ItemClass is just a bunch of getters and setters.


And lastly, my upDateDataBase Method inside my DataBaseHelper Class.



public void upDateDataBase(ItemClass itemClass){
SQLiteDatabase dbB = this.getWritableDatabase();
SQLiteDatabase dbC = this.getWritableDatabase();
SQLiteDatabase dbH = this.getWritableDatabase();
SQLiteDatabase dbM = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();
contentValues.put(ITEM_NAME, itemClass.getName());
contentValues.put(KEY_STATUS, itemClass.getStatus());

dbB.update(TABLE_NAME_ONE, contentValues, KEY_ID + " = ?", new String[]{String.valueOf(itemClass.getId())});
dbC.update(TABLE_NAME_TWO, contentValues, KEY_IDC + " = ?", new String[]{String.valueOf(itemClass.getId())});
dbH.update(TABLE_NAME_THREE, contentValues, KEY_IDH + " = ?", new String[]{String.valueOf(itemClass.getId())});
dbM.update(TABLE_NAME_FOUR, contentValues, KEY_IDM + " = ?", new String[]{String.valueOf(itemClass.getId())});
}


Thank you for your time.


Aucun commentaire:

Enregistrer un commentaire