Hi There,
I have written a small android app that grabs some data and displays it in a ListView.
I want to have the list sorted in ascending order (no prob). But when I add a new item it stays at the bottom of the list even when the view/app is reloaded. It seems the only data that is being sorted is the entries that were created in the database OnCreate() method.
I'm not sure what code to provide but i'll start by showing you snippets of the DBAdapter class, then from the main app class.
Database construction query:
private static final String DATABASE_CREATE =
"create table "+ DATABASE_TABLE
+ "("
+ FIELD_ROWID+" integer primary key autoincrement, "
+ FIELD_NAME +" varchar(30) not null, " + FIELD_TELEPHONE + " varchar(20) not null, "
+ FIELD_ADDRESS + " text not null,"
+ FIELD_URL + " varchar(255) not null);";
Function to get list of data:
public Cursor getRestaurants()
{
Cursor result = null;
try{
result = db.query(DATABASE_TABLE, new String[] {FIELD_ROWID, FIELD_NAME}, null, null, null, null, FIELD_NAME+" ASC");
}
catch (Exception e)
{
Log.v("applog", e.getMessage());
}
return result;
}
Here is a snippet of the main method. Here you can see that I am calling the getRestaurants() function shown above. I'm stumped as to why the data isn't being sorted.
Any advice greatly appreciated. Thanks in advance.
public class Assignment2 extends ListActivity {
// Set up some global variables.
private static final int ADD_NEW_ID = Menu.FIRST;
private static final int CLOSE_APP_ID = ADD_NEW_ID+1;
private final DBAdapter db = new DBAdapter(this);
private static CursorAdapter curAdapter = null;
private static Cursor rawData = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db.open(); // Open connection to restaurant db.
rawData = db.getRestaurants();
Log.v("applog", rawData.getCount()+" Restaurants loaded from DB.");
if(rawData.getCount() == 0)
{
Toast.makeText(getApplicationContext(), "No restaurants found.", Toast.LENGTH_SHORT).show();
}
try
{
/* The following two arrays are used to map DB fields to ListView items
* From a custom layout file.
*/
// Array of db fields to be used when converting cursor to ListView
String[] strFields = { "name", BaseColumns._ID };
// Array of listview id's to be used when populating the ListView
int[] intFields = { R.id.first };
curAdapter = new SimpleCursorAdapter(this, R.layout.row, rawData, strFields, intFields);
}
catch (Exception e)
{
Log.v("applog", e.getMessage());
}
// Apply the converter cursor to the current ListView
setListAdapter(curAdapter);
db.close(); // Close connection to restaurant db.