Reputation: 83
I have a listView which is populated by Data from my Database. If a user clicks on an item on my listview, he can update the data on that item. The problem now is, only the first item on the listview can be updated. Here is my code:
private ListView.OnItemClickListener listContentOnItemClickListener
= new ListView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Cursor cursorclick = (Cursor) parent.getItemAtPosition(position);
final String item_id = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_ID));
String item_score = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_Score));
final String stud_id = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_StudentID));
AlertDialog.Builder myDialog
= new AlertDialog.Builder(Scores_Student_Profile.this);
myDialog.setTitle("Update Score");
TextView dialogTxt_id = new TextView(Scores_Student_Profile.this);
LayoutParams dialogTxt_idLayoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
dialogTxt_id.setText("#" + String.valueOf(item_id));
final EditText dialogC1_id = new EditText(Scores_Student_Profile.this);
LayoutParams dialogC1_idLayoutParams
= new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
dialogC1_id.setLayoutParams(dialogC1_idLayoutParams);
dialogC1_id.setText(item_score);
LinearLayout layout = new LinearLayout(Scores_Student_Profile.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(dialogTxt_id);
layout.addView(dialogC1_id);
myDialog.setView(layout);
myDialog.setPositiveButton("Update", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
String value1 = dialogC1_id.getText().toString();
//I tried to make a toast here to see if the data is correct. The variables shows the intended values
Toast.makeText(getApplicationContext(), " "+mRowId+" "+critspnrSelected+" "+item_id+" "+value1+" "+stud_id, Toast.LENGTH_LONG).show();
scoresDataBaseAdapter.update_byID(mRowId,critspnrSelected,item_id,value1,stud_id);
updateList();
}
});
myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
});
myDialog.show();
}};
private void updateList(){
cursorShowScore.requery();
}
}
The code below is how I display the ListView:
if (mRowId != null) {
cursorShowScore = scoresDataBaseAdapter.queueSpnrScore(mRowId, critspnrSelected,studID);
String[] from = new String[]{ScoresDbAdapter.KEY_ID,ScoresDbAdapter.KEY_Score};
int[] to = new int[]{R.id.critnumberof,R.id.studscoreon};
cursorAdapter = new SimpleCursorAdapter(this, R.layout.score_students_profile_criteria_row, cursorShowScore, from, to);
listContent.setAdapter(cursorAdapter);
listContent.setOnItemClickListener(listContentOnItemClickListener);
}
}
and this is how I do the update:
public void update_byID(String table,String crit,String id, String score,String studid){
String tab = table+"_"+crit+"_scores";
ContentValues values = new ContentValues();
values.put(KEY_Score, score);
mDb.update(tab, values,
KEY_ScoreID + " = ? and "+ KEY_StudentID + " = ?",
new String[] { id,studid });
}
any help will be appreciated.
Upvotes: 0
Views: 162
Reputation: 83
It turned out that I passed a wrong variable in my update_byID.I solved the problem by declaring
final String score_id = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_ScoreID));
and my changing my update call to:
scoresDataBaseAdapter.update_byID(mRowId,critspnrSelected,score_id,value1,stud_id);
Upvotes: 1