vendredi 13 mars 2015

read data sqlite in android failed

I have a problem in my application,

the purpose is to read the data stored in database,

but it end up showing the unexpected value.


here is my ViewData.class



public class ViewData extends ListActivity {

//inisialisasi kontroller
private DBDataSource dataSource;

//inisialisasi arraylist
private ArrayList<Wiridan> values;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewdata);

dataSource = new DBDataSource(this);
// buka kontroller
dataSource.open();

// ambil semua data barang
values = dataSource.getAllWiridan();

// masukkan data barang ke array adapter
ArrayAdapter<Wiridan> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, values);

// set adapter pada list
setListAdapter(adapter);
}
}


code in DBDataSource.java for reading data



private Wiridan cursorToWiridan(Cursor cursor)
{
// buat objek barang baru
Wiridan wiridan = new Wiridan();
// debug LOGCAT
Log.v("info", "The getLONG "+cursor.getLong(0));
Log.v("info", "The setLatLng "+cursor.getString(1)+","+cursor.getInt(2));

/* Set atribut pada objek barang dengan
* data kursor yang diambil dari database*/
wiridan.setId(cursor.getLong(0));
wiridan.setJudul_wiridan(cursor.getString(1));
wiridan.setJumlah_wiridan(cursor.getInt(2));

//kembalikan sebagai objek barang
return wiridan;
}


//mengambil semua data barang

public ArrayList<Wiridan> getAllWiridan() {
ArrayList<Wiridan> daftarWiridan = new ArrayList<>();

// select all SQL query
Cursor cursor = database.query(DBHelper.TABLE_NAME,
allColumns, null, null, null, null, null);

// pindah ke data paling pertama
cursor.moveToFirst();
// jika masih ada data, masukkan data barang ke
// daftar barang
while (!cursor.isAfterLast()) {
Wiridan wiridan = cursorToWiridan(cursor);
daftarWiridan.add(wiridan);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return daftarWiridan;
}


And the last is my entity class Wiridan.java



public class Wiridan {
private long id;
private String judul_wiridan;
private int jumlah_wiridan;

public Wiridan()
{

}
public long getId() {
return id;
}


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

public String getJudul_wiridan() {
return judul_wiridan;
}


public void setJudul_wiridan(String judul_wiridan) {
this.judul_wiridan = judul_wiridan;
}

public int getJumlah_wiridan() {
return jumlah_wiridan;
}


public void setJumlah_wiridan(int jumlah_wiridan) {
this.jumlah_wiridan = jumlah_wiridan;
}

}


and the result is unexpected, it just showed value of package,

not the value i've input from ResultActivity.java

Unexpected Value




I really Appreciate your help, and sorry for my bad english.


Aucun commentaire:

Enregistrer un commentaire