android replacing nulls with TBD in listview - android

SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, R.layout.mytaskslayout, cursor, new String[] {"Aircraft","Discrepancy", "ARR_FLT", "ARR_Gate", "DPT_FLT", "DPT_Gate"}, new int[] {R.id.ac, R.id.discrepancy, R.id.arrac, R.id.arrgate, R.id.dptac, R.id.dptgate});
Here some of the values are null. I don't want to display null but TBD. Can I do something here. (I CAN NOT CHANGE SQL)

iterate through your array (populating the listview) and any value in the array that equals null make it equal TBD
but from what im seeing your creating your arrays on the fly and not from a database
new String[] {"Aircraft","Discrepancy", "ARR_FLT", "ARR_Gate", "DPT_FLT", "DPT_Gate"}, new int[] {R.id.ac, R.id.discrepancy, R.id.arrac, R.id.arrgate, R.id.dptac, R.id.dptgate}

Related

How to navigate through data via Sqllite and ListActivity

I'm displaying data from SQLite using
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
employees,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
getListView().setAdapter(adapter);
empoyees - my Cursor.
Data is showing correctly, but how to get what row I used from SQLite duaring OnClick on my item from List?
For example I have 2 tables
1)Category
_id Integer
Name Text
2)Articles
_id Integer
Name text
CategoryId (foreing key)
So on first screen I'm displaying all Categorys, then on list item click I want to display Articles that are from specified category, how to do that?
Don't use your cursor in your adapter. Build up an array and send in the array instead. In that array, typically something like List<MyDataRecordHolder> (where MyDataRecordHolder is an object you've created and filled with data from the database), you will have to set a reference to a row id.
UPDATE:
Create your MyDataRecordHolder: (Pseudo code)
List<MyDataRecordHolder> list = new List<MyDataRecordHolder>();
in your loop where you fetch data:
MyDataRecordHolder record = new MyDataRecordHolder();
record.setId(cursor.get("rowid"))
//set any other pertinent data
list.add(record);
For your adapter:
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
list,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
Note, you are now supplying the List (list) to the adapter.
In your OnClick for your row, in the adapter, you will get the current MyDataRecordHolder:
final MyRecordHolder record = getItem(position);
String id = record.getId();
//make new db query based on your id

Fill each spinner row with two values from database

I have a spinner currently being filled with one column (name) from a database. I would like to change this to have each spinner row displaying the name, a space and an Id from the same table. This is the code I am using at the moment, I am not sure how to map multiple rows in the "from" array into the same textview in the "to" array.
Cursor c = db.fetchAllUsers();
startManagingCursor(c);
// create an array to specify which fields we want to display
String[] from = new String[]{"name"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
spinner.setAdapter(adapter);
Thanks!
Use SimpleCursorAdapter.setViewBinder with a custom ViewBinder that does what you wish. To elaborate, you can simply ignore columnIndex in your implementation of setViewValue and use the given Cursor to populate the given View however you wish.
See: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)

How to Retrieve the data from the server

After the successful connection to the server(my local host),i want to retrieve the data that server sends to me. Can anyone please suggest me what should i do for this?
After receiving that data, i have to store it into an Array and then have to fetch it one by one into the dropdown list.
Thanks
Sheetal i have not worked with any application with server,but i retrieved data locally and then placed it in an array and showed in the spinner, this might help u
Cursor c = db.fetchAllReminders();
startManagingCursor(c);
// Create an array to specify the fields we want (only the TITLE)
String[] from = new String[]{DataManager.NAME};
// and an array of the fields we want to bind in the view
int[] to=new int[] { android.R.id.text1 };
// to[0]=android.R.id.text1;
final Spinner spinner = (Spinner) this.findViewById(R.id.spins);
// Now create a simple cursor adapter and set it to display
final SimpleCursorAdapter reminders =new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, c, from, to);
spinner.setAdapter(reminders);

Populating a spinner with a db query

I recently started programming for android and java development in general. Currently I train by writing a timetable app.
I want to get a list with all subjects from the db (the table also contains teacher and rooms) and put them into a spinner.
Thats the code I wrote
Cursor c = dba.fetchAllSubjects();
if (c.getCount() != 0) {
Spinner subjectSpinner = (Spinner) findViewById(R.id.newlesson_subject);
startManagingCursor(c);
String[] from = new String[]{DbAdapter.KEY_SUBJECT};
int[] to = new int[] {android.R.layout.simple_spinner_item};
SimpleCursorAdapter subjectAdapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
subjectSpinner.setAdapter(subjectAdapter);
}
The problem is that when clicking on the spinner it shows a list with as much items as the db has entries, but the list doesn't show the names (you can say it's empty). So I probably messed something up with the simple cursor adapter, but I don't know what.
Thanks for your help
You're error lies in specifying the wrong Resource ID in your to integer array.
int[] to = new int[] {android.R.layout.simple_spinner_item};
You need to specify the ID of a TextView (or similarly typed view) and not the layout itself. Replace the line above with:
int[] to = new int[] { android.R.id.text1 };

Fetch all notes returns zero rows from Android notepad example

Well this is probably a stupid question with a simple answer but when using simple cursor adapter from the notepad example, I get a list of names from my database.
When I try to do it "manually" by moving the cursor over the rows and extracting the names the cursor says there is zero rows returned...
This works as per the example:
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only NAME)
String[] from = new String[]{WeightsDatabase.KEY_NAME};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.weightrows};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.weights_row, notesCursor, from, to);
setListAdapter(notes);
Now i'm trying to do this but it's not quite working. Is the simple cursor adapter doing something special i'm not?
//check # of rows, returns 0
int mOne = notesCursor.getCount();
//initial random string array
String[] temp = new String[100];
int i = 0;
notesCursor.moveToFirst();
do{
temp[i]=notesCursor.getString(notesCursor.getColumnIndex(WeightsDatabase.KEY_ROWID)); //crashes here
//index out of bounds
i++;
}while(notesCursor.moveToNext());
I have gotten this to work, but with returning a specific query like return all row with the name "_". What is different about returning all notes?
moveToFirst() actually returns a boolean, so you can prevent the exception from being thrown by checking the value before you attempt to read from the Cursor:
if (notesCursor.moveToFirst()) {
// do loop
}
As for why there are 0 rows, are you attempting to re-use the same cursor that you passed into the SimpleCursorAdapter, or is the code that is failing stand-alone? If you are attempting to re-use it, I would try it using a new Cursor after performing a fresh fetchAllNotes().

Categories

Resources