Search code examples
javaandroidsqlitelistviewsqliteopenhelper

I can not see list items in ListView


I am writing simple phone contact. I add people's names,phones,e-mails. Only names must be shown in listView. I can see list's row, but can not see names on rows. They are invisible.

This method is showing infos of people:

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()){
        String isim = c.getString(c.getColumnIndex("isim"));
        Kontak knt = new Kontak();
        knt.setIsim(isim);
        list.add(knt);
        c.moveToNext();
    }
    c.close();
    db.close();
    return list;
}

This is my data class:

public class Kontak {

int id;
String isim;
String tel;
String mail;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getIsim() {
    return isim;
}

public void setIsim(String isim) {
    this.isim = isim;
}

public String getTel() {
    return tel;
}

public void setTel(String tel) {
    this.tel = tel;
}

public String getMail() {
    return mail;
}

public void setMail(String mail) {
    this.mail = mail;
}

public String toString(){return isim;}}

This is adapter part:

    k = new Kontak();
    dbHelper = new DbHelper(this);
    list = dbHelper.kisiOku();
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
    listView.setAdapter(adapter);

Solution

  • In your code snippet you are querying from the SQLite and updating isim (which I assume to be name field) in the Kontak object. The same object is being appended to the list. The other fields like mail and phone are not being updated in the Kontak object.

    Also, the toString method of Kontak class returns only isim. Consider adding mail and phone number details to the output string.