Reputation: 13
I am writing like contact. I add people's infos(name,phone,mail) and I can see them in listView
. People who are added list, has specific ID numbers. I want to update their infos by using ID numbers. But infos are not updated. They are still old version.
These are update and read info methods in DbHelper:
public List<Kontak> kisiOku(){
SQLiteDatabase db = this.getReadableDatabase();
List<Kontak> list = new ArrayList<>();
String[] kolon = {"id","isim","tel","mail"};
Cursor c = db.query("kisi",kolon,null,null,null,null,null);
c.moveToFirst();
while (!c.isAfterLast()){
int id = c.getInt(0);
String isim = c.getString(1);
String tel = c.getString(2);
String mail = c.getString(3);
Kontak k = new Kontak(isim,tel,mail);
k.setId(id);
list.add(k);
c.moveToNext();
}
c.close();
db.close();
return list;
}
public void kisiDuzelt(String isim,String tel,String mail){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues val = new ContentValues();
val.put("isim",isim);
val.put("tel",tel);
val.put("mail",mail);
Kontak k = new Kontak(isim, tel, mail);
db.update("kisi", val, "id = "+k.getId(),null);
}
Update method is used:
public void duzelt(View v){
String isim = editName1.getText().toString();
String tel = editTel1.getText().toString();
String mail = editMail1.getText().toString();
dbHelper.kisiDuzelt(isim,tel,mail);
new Kontak(isim,tel,mail).getId();
Toast.makeText(getApplicationContext(),"DÜZELTİLDİ",Toast.LENGTH_LONG).show();
}
This is adapter part:
dbHelper = new DbHelper(this);
list = dbHelper.kisiOku();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
Upvotes: 0
Views: 36
Reputation: 152867
Kontak k = new Kontak(isim, tel, mail);
This does not initialize the id field in your Kontak object, and getId()
later returns whatever default value such as 0. That does not match anything in the database and no rows get updated.
You have to either pass around the id retrieved from the database all the way, or those kind of Kontak objects that have an id populated to match the value in your database.
Upvotes: 1