I am new to android. I want to show inserted data from database to a table. I have used List to get the data.
public List<user> getAllUsers() {
List<user> users = new ArrayList<user>();
Cursor cursor = database.query(SqlLiteHelper.TABLE_USER, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
user varUser = cursorToUser(cursor);
//Log.v("user name",varUser.getName().toString());
users.add(varUser);
cursor.moveToNext();
}
//Log.v("user name",users.get(0).getName().toString());
cursor.close();
return users;
}
private user cursorToUser(Cursor cursor) {
user varUser = new user();
//Log.v("user id", cursor.getString(1).toString());
varUser.setID(cursor.getLong(0));
varUser.setName(cursor.getString(1));
varUser.setPhone(cursor.getString(2));
varUser.setEmail(cursor.getString(3));
return varUser;
}
I wrote this code on UserDataSource.java
Then where i need to show those data i use the following codes
databaseSource = new UserDataSource(this);
databaseSource.open();
List<user> values = databaseSource.getAllUsers();
Log.v("information", values.get(0).getName().toString());
ArrayAdapter<user> adapter = new ArrayAdapter<user>(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
This is not adding the values in the list. I use the log.v to check whether the data is coming back or not. And i found that the data is coming perfectly from the database. Only when i try to print the List or insert it to the simple_list_item_1 then it is doing the problem.
It is printing
com.example.given_n_take.user@41221338
Buts its suppose to print the name from the database.
Database table name is user.
Please let me know what am i missing.
here is my xml file of layout where i am trying to add those list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/all_user_back"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Back" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="384dp" >
</ListView>
</LinearLayout>
If your list shows
com.example.given_n_take.user@41221338this means that you are missing atoStringmethod in youruserclass. Implement atoStringthat returns the name of the user.