Basicly I have two tables in my database brought together with a rawQuery and a ListActivity that is supposed to pull info from both tables and show it inside of the ListActivity. I don't get a force close or anything the problem is the information does not show up.
MY RAWQUERY
public Cursor fetchName() {
return ourdb.rawQuery("SELECT wwJobTable._id, wwJobTable.JobName, wwLatLonTable.JobLatitude FROM wwJobTable, wwLatLonTable WHERE wwLatLonTable.JobLatitude=wwJobTable.JobCode", null);
}
HOW I AM VIEWING THE TABLE INFORMATION
private void fillData() {
// Get all of the rows from the database and create the item list
mTimeNotesCursor = mDbHelper.fetchName();
mTimeNotesCursor.moveToFirst();
startManagingCursor(mTimeNotesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{WWDatabase.KEY_JOBNAME,
WWDatabase.KEY_LATITUDE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.textView1, R.id.textView2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.time_text_row, mTimeNotesCursor, from, to);
setListAdapter(notes);
}
What could I possibly be doing wrong?
Related
I would like to change it by getting the data from the database .
public void initList(){
product = new String[]{"apple","apricot","banana","orange","nuts","pears","pineapple","watermelon"};
listProducts = new ArrayList<>(Arrays.asList(product));
adapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.txtitem, listProducts);
listView.setAdapter(adapter);
}
For lists that come from the database, you should use a CursorAdapter
public void initList() {
String[] from = new String[] { productColName };
int[] to = new int[] { R.id.txtitem };
Cursor cursor = db.query(.....);
CursorAdapter adapter = new SimpleCursorAdapter(context, R.layout.list_item, cursor, from, to, 0);
listView.setAdapter(adapter);
}
This is an easy way to handle database data for a list item with all TextViews. If you have more complex data, such as for images etc. you should create a CursorAdapter subclass and override newView() and bindView().
You can use SQLite db, which is commonly used in android apps. You can insert and retrieve the data using SQLite queries.
For your example: you can create a table consisting the names of the fruits.
You can query your database to get all the fruits and populate the list (by adding the retrieved results in to the list) and use it as you are using it now to inflate the listView.
You can use the following tutorial to know about how to create a table, insert/update an entry and retrieve data from the table.
SQLite setup and sample queries
Good day all, Just wondering on a tiny dilemma . I have a custom CursorLoader that loads all items from an SqliteDatabase in the loadInBackground.
#Override
public Cursor loadInBackground() {
final String[] TIMER_PROJECTION = new String[]{
MyDatabaseModule.KEY_ID, MyDatabaseModule.KEY_NAME, MyDatabaseModule.KEY_GROUP, MyDatabaseModule.KEY_TASK};
Cursor cursor = dbAdapter.getAllItems(); //method in Database
return cursor;
}
if you want to get data for items with specific group name like dbAdapter.getItems(String GroupName) or get on the column groups like dbAdapter.getAllGroups(), Would creating a new Custom CursorLoader() for all this be too much or would it be better to just stick to dbAdapter.getAllItems() as shown in the code and manipulate the cursor from there? Thank you
I am having an issue with a listview refresh, I have a refresh function that works on load and when I click a button tied to it. What it will not do is refresh after I add info to the SQLite database and call the refresh function.
//Add item to order Table.
public void addItemToOrder()
{
itemNumberValue = qoItemNumber.getText().toString();
orderQtyValue = qoOrderQty.getText().toString();
itemDescValue = qoItemDesc.getText().toString();
Intent searchItem = new Intent(getApplicationContext(),inputOrder.class);
searchItem.putExtra("itemnumb", itemNumberValue);
searchItem.putExtra("orderqty", orderQtyValue);
searchItem.putExtra("qoitemdesc", itemDescValue);
startActivity(searchItem);
fillData();
}
//Load item table to listview
public void fillData() {
// Get all of the rows from the database and create the item list
mNotesCursor = mDbHelper.fetchAllOrders();
startManagingCursor(mNotesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{HDWDBHelper.KEY_ITEMNUM, HDWDBHelper.KEY_DESC, HDWDBHelper.KEY_QTY};
// and an array of the fields we want to bind those fields to (in this case just text1)
//int[] to = new int[]{R.id.text1};
int[] to = new int[]{R.id.tvItemNum, R.id.tvItemDesc, R.id.tvQty};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.itemlist, mNotesCursor, from, to);
notes.notifyDataSetChanged();
setListAdapter(notes);
notes.notifyDataSetChanged();
Log.v("fillData", "In fillData");
}
When I call fillData() at onCreate() or on button click, it works but when I call it in the addItemToOrder() function, nothing happens. The listview still shows previous items listed until I click the refresh button then it shows the new item added.
I'm using SimpleCursorAdaptor and a ListView to display the values in my SQLite database rows. One of the values in my row is a date (column 'date'). Rather than display this date I need to run this through a method that will return another string based on what the date is. This is the value I want displayed in my list rather than the actual value taken straight from the Database.
In short I wish to display all values from my database table row except for one, where I need to change it before displaying it.
Here is my code:
public class BinCollectionDayListActivity extends ListActivity{
//
private static final String fields[] = { "name", "date", BaseColumns._ID };
//
public void onCreate(Bundle savedInstanceState) {
//
super.onCreate(savedInstanceState);
//
DatabaseHelper helper = new DatabaseHelper(this);
SQLiteDatabase database = helper.getWritableDatabase();
Cursor data = database.query("names", fields, null, null, null, null, null);
CursorAdapter dataSource = new SimpleCursorAdapter(this, R.layout.binrow, data, fields, new int[] { R.id.name, R.id.date });
dataSource.getCursor().requery();
//
ListView view = getListView();
view.setHeaderDividersEnabled(true);
setListAdapter(dataSource);
//
helper.close();
database.close();
}
}
As you can tell I am pretty new to Android development and would love to know what the best approach would be to achieving the desired result.
Thanks in advance,
Tony
Two options that I've used before:
Array Adapter (Preferred):
Create an ArrayAdapter and populate the Cursor data into your ArrayAdapter.
http://anujarosha.wordpress.com/2011/11/17/how-to-create-a-listview-using-arrayadapter-in-android/
ViewBinder: On your Cursor, you can setup/specify a ViewBinder where you can check the data that's about to be mapped, perform some logic on it, and spit out a different result if desired. This is probably exactly what you're looking for, but do consider the ArrayAdapter as it tends to give you better control and it's a pain to switch these things later on.
Changing values from Cursor using SimpleCursorAdapter
I am using two SimpleCursorAdapters to display text and images in a row in my Listview. The problem is that when calling these Adapters two times they conflict with each other. In the end my Listview only shows the data of the SimpleCursorAdapter that I called last.
What I need to do is merge those two SimpleCursorAdapters although they use different sql databases.
Any ideas to solve this??
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_list);
mDbHelper = new RemindersDbAdapter(this);
mImageHelper = new ImageAdapter(this);
mDbHelper.open();
mImageHelper.open();
fillData();
fillImages();
registerForContextMenu(getListView());
}
//
// Fills the ListView with the data from the SQLite Database.
//
private void fillData() {
Cursor remindersCursor = mDbHelper.fetchAllReminders();
startManagingCursor(remindersCursor);
// Creates an array with the task title.
String[] from = new String[] {RemindersDbAdapter.KEY_TITLE, RemindersDbAdapter.KEY_BODY};
// Creates an array for the text.
int[] to = new int[] {R.id.text1, R.id.text2};
// SimpleCursorAdapter which is displayed.
SimpleCursorAdapter reminders = new SimpleCursorAdapter(this, R.layout.reminder_row, remindersCursor, from, to);
setListAdapter(reminders);
}
//
// Fills the ListView with the images from the SQLite Database.
//
private void fillImages() {
Cursor imageCursor = mImageHelper.fetchAllImages();
startManagingCursor(imageCursor);
// Creates an array with the image path.
String[] fromImage = new String[] {ImageAdapter.KEY_IMAGE};
// Creates an array for the text.
int[] toImage = new int[] {R.id.icon};
// SimpleCursorAdapter which is displayed.
SimpleCursorAdapter images = new SimpleCursorAdapter(this, R.layout.reminder_row, imageCursor, fromImage, toImage);
setListAdapter(images);
}
You can use the MergeCursor class to expose a number of individual cursors as a single cursor. As your adapters bind different columns->widgets, you may have to write your own subclass of SimpleCursorAdapter (or just CursorAdapter) so that you can do the correct type of binding depending on the row.
how about creating your own adapter (extend BaseAdapter) , which will point to the correct data each time it needs to ?
this way you can have full control over when and where to show each item.
you can even use the 2 adapters and use them for the new adapter.
What I need to do is merge those two SimpleCursorAdapters although they use different sql databases.
If the two adapters are truly different (e.g., different row layouts), use my MergeAdapter to combine your two existing adapters into one, then use the MergeAdapter with your ListView.
However, in your case, it would appear that you simply have two Cursors with different contents, in which case superfell's answer of using MergeCursor is probably a better approach.