How do you Populate ListView With SQLite? - android

My program crashes every time I try to run this method:
private void populateListView() {
Cursor cursor = myDB.getAllRows();
startManagingCursor(cursor);
String[] from = new String[] {ProductContract.ProductEntry.INPUT, ProductContract.ProductEntry.DATE};
int[] to = new int[] {R.id.t_input, R.id.t_date};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.display_row, cursor, from, to, 0);
ListView myList = (ListView) findViewById(R.id.display_listview);
myList.setAdapter(myCursorAdapter);
}
Here's my getAllRows method:
public Cursor getAllRows() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.query(true, ProductContract.ProductEntry.TABLE_NAME, ProductContract.ProductEntry.ALL_KEYS,
null, null, null, null, null, null);
if(c != null) {
c.moveToFirst();
}
return c;
}

Related

Android: How to create a custom array Adapter to show two textviews from database

I need help in showing two values from database in my listview which I am populating through array Adapter but default array Adapter shows only 1 value I want to show two values. Below is my code of how i am populating the array adapter through db.
private void updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
final Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
null, null, null, null, null);
while (cursor.moveToNext()) {
int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
taskList.add(cursor.getString(idx));
}
mAdapter = new ArrayAdapter<>(this,
R.layout.item_todo,
R.id.task_title,
taskList);
mTaskListView.setAdapter(mAdapter);
cursor.close();
db.close();
}
Try below code :
private void updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
final Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
null, null, null, null, null);
if(cursor.getCount()>0 && cursor.moveToFirst()){
while (cursor.moveToNext()) {
int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
taskList.add(cursor.getString(idx));
}
}
mAdapter = new ArrayAdapter<>(this,
R.layout.item_todo,
R.id.task_title,
taskList);
mTaskListView.setAdapter(mAdapter);
cursor.close();
db.close();
}

Get values from database to ListView

I have written this fetchData() method to fill ListView with the values from database.
public void fetchData() {
database = helper.getReadableDatabase();
Cursor c = database.query(db.TABLE, null, null, null, null, null, null);
String[] fromDB={db.FULLNAME,db.YEAR};
int[] toView={R.id.tvName_lv,R.id.tv_birthday_lv};
adapter = new SimpleCursorAdapter(this, R.layout.listview_all, c, fromDB, toView);
lv.setAdapter(adapter);
}
It works fine. But when I try to use it this way, it gives an error.
public void fetchData() {
database = helper.getReadableDatabase();
Cursor c = database.query(db.TABLE, null, null, null, null, null, null);
String birthday=db.YEAR+" "+db.MONTH+" "+db.DAY;
String[] fromDB={db.FULLNAME,birthday};
int[] toView={R.id.tvName_lv,R.id.tv_birthday_lv};
adapter = new SimpleCursorAdapter(this, R.layout.listview_all, c, fromDB, toView);
lv.setAdapter(adapter);
}
I want to get [db.YEAR+" "+db.MONTH+" "+db.DAY] to the view [R.id.tv_birthday_lv]
Thanks in advance.
UPDATE:Error is "Unfortunately, App has stopped". FATAL EXCEPTION on LogCat

Getting multiple entries for SELECT DISTINCT sqlite android?

I am trying to get the unique column name and show it as a list adapter.
public String[] getAllUsers() {
String[] contactList = new String[100];
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(true,TABLE_TRANS, new String[] { KEY_ID ,KEY_NAME, KEY_CASH,KEY_DESC,KEY_DATE }, null, null, KEY_NAME, null, null, null);
int i=0;
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
contactList[i] = cursor.getString(1);
i++;
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
The above is method in my database class.
And from mainActivity I call this method
private void updateUserList() {
// TODO Auto-generated method stub
String[] contactList = new String[100];
contactList = db.getAllUsers();
Log.d("User List",contactList[0]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, contactList);
setListAdapter(adapter);
}
The list is getting updated ,but list is getting repeated as a whole
I don't know if I fully understood your question.
If you want to get columns KEY_ID ,KEY_NAME, KEY_CASH,KEY_DESC,KEY_DATE from all rows in database without KEY_NAME repetitions use this query:
Cursor cursor = db.query(true,TABLE_TRANS,
new String[] { KEY_ID ,KEY_NAME, KEY_CASH,KEY_DESC,KEY_DATE },
null, null, null, null, null, null);
In your methods do not use Array instead use ArrayList<String>
public ArrayList<String> getAllUsers() {
ArrayList<String> contactList = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(true,TABLE_TRANS,
new String[] { KEY_ID ,KEY_NAME, KEY_CASH,KEY_DESC,KEY_DATE },
null, null, null, null, null, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
contactList.add(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
And updateUserList():
private void updateUserList() {
// TODO Auto-generated method stub
ArrayList<String> contactList;
contactList = db.getAllUsers();
Log.d("User List", contactList.get(0));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, contactList);
setListAdapter(adapter);
}
As you only use the column KEY_NAME from cursor the query can be simplified to return only that column.
Cursor cursor = db.query(true,TABLE_TRANS,
new String[] {KEY_NAME}, null, null, null, null, null, null);
The problem was with initializing the String array as 100. I wrote a method to get the total number of counts and it works fine.
public int getUsersCount() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(true,TABLE_TRANS, new String[] { KEY_ID ,KEY_NAME, KEY_CASH,KEY_DESC,KEY_DATE }, null, null, KEY_NAME, null, null, null);
//cursor.close();
// return count
return cursor.getCount();}

Concatenate 2 columns Android SQLite

So far I have this:
Cursor cursor = dbHelper.getAllPatients();
String[] data = new String[]{DBHelper.KEY_LNAME,
DBHelper.KEY_FNAME, DBHelper.KEY_DIAGNOSIS};
int[] to = new int[] {R.id.fullname, R.id.diagnosis};
dataAdapter = new SimpleCursorAdapter(this, R.layout.custom_row, cursor, data, to, 0);
dbHelper.close();
lv.setAdapter(dataAdapter);
lv.setTextFilterEnabled(true);
getAllPatients() method
public Cursor getAllPatients()
{
Cursor localCursor =
this.myDataBase.query(DB_TABLE, new String[] {
KEY_ID, KEY_FNAME, KEY_LNAME, KEY_DIAGNOSIS }, null, null, null, null, null);
if (localCursor != null)
localCursor.moveToFirst();
return localCursor;
}
I want the columns FNAME, and LNAME to be as one but I'm confused how and where to put the concatenate operator + in the String array. Do you have any idea to do this? I would gladly appreciate your help. Thanks.
Do the following when you query
Cursor localCursor =
this.myDataBase.query(DB_TABLE, new String[] {
KEY_ID, KEY_FNAME +"||"+ KEY_LNAME, KEY_DIAGNOSIS }, null, null, null, null, null);
And while assigning values using your adapter do as follows,
String[] data = new String[]{DBHelper.KEY_LNAME +"||"+ DBHelper.KEY_FNAME, DBHelper.KEY_DIAGNOSIS};
int[] to = new int[] {R.id.fullname, R.id.diagnosis};
try this
SELECT CONCAT(ColA, ColB) AS ColC FROM Table
or
String query = "select" +ColA+"+"+ColB +"as CompleteAddress from YourTable";

ListView with SimpleCursorAdapter

in my app i am using a listview to display a menu of dishes with their Dish Names
This is the code where i bind the data from the query to the listview using the adapter:
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
cursor.moveToFirst();
Log.w("ppp","kk - " + cursor.moveToFirst());
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}
My Query code looks like this:
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
Really appreciate any help/comments as i have been stuck on this for almost a week now.
Can you explain explicitely what's your problem?
My suggest change
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
to
public Cursor getAllDishes()
{
Cursor dishes = db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
if(dishes != null)
dishes.moveToFirst();
return dishes;
}
Then
in getDishes() method write like this
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(YourActivity.this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}

Categories

Resources