I'm a new Android Developer and seem to have gotten in a little over my head. I am trying to make a listView update when I add more content to the list.
The ListView is based off of a SQLite database. I was able to get the ListView to be based on the SQLite database by making a ContentProvider for the SQLite database (which was suggested here). Now my issue is that I want to update the SQLite database and have it reflected on the ListView. I am using a loader and according to this if I implemented the loader right it will monitor the data.
I tried updating the SQLite database directly but, that didn't cause the ListView to update without closing and reopening. My instinct from there is that I should implement the insert method in my contentProvider. I did a very simple implementation:
#Override
public Uri insert(Uri uri, ContentValues contentValues) {
return ContentUris.withAppendedId(uri, mCollectionDB.insertCollection(contentValues));
}
Unfortunately, the result of this is my app crashes with a "java.lang.NullPointerException". This is especially confusing as using the exact same contentValues to make the .insertCollection call from my MainActivity works without issue.
The issue I'm really interested in is how to get my listView to update when I insert data into my SQLite database. If inserting into the ContentProvider is irrelevant then please ignore that. I'm not really sure where I went wrong so I'm not sure what other code may be useful, but I'm happy to edit in more code if it'll help.
You can check loader concept.
You can start with
http://www.vogella.com/articles/AndroidSQLite/article.html#todo
Following are other 2 good tutorials
http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/
http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/
Hey here is one example to insert data into sqlite database and display it in list view.
Have a look at it
The idea is simple insert data into database and on click of view button initialize List view with arraylist that contains data already inserted.
You can ask if you have any further queries.
Related
I want to retrieve data from a MySQL database and populate a ListView in my Android application. I've seen answers on stackoverflow that use complicated methods of retrieving the data from a MySQL database using JSON objects. Instead, is there anyway I can use something similar to a MySqliteCursor to connect to a MySQL database? Is the Cursor Android class limited to using SQLite databases? If none of these options are possible, is there an easier way to populate a ListView with MySQL data?
See CursorAdapter class. It receive cursor, db column indicies and ids of widgets of view. Then it take data from db and populate corresponding fields
Yes, it receive Cursor from sqlite. You can easily implement adapter for any data- just know number of rows and query row under particular index. See RecyclerView tutorial (updated listview)
I have a HTTP Server and Android app.
All data -> JSON format. For mapping used Gson.
ORM - ActiveAndroid.
The problem : I need something like an observer/notifier object, which can told me, that a row in database updated just now.
Something like this :
public void interface Observable<T> {
void onItemUpdated(T item);
}
So I looking a solution. I've read ActiveAndroid docs, but it doesn't get to me any result. Maybe i can mix something with ContentObserver or something like this?
How about to create a logging table where you can append a row for each update.
It has the insertion cost but select query is so fast. And also you can store update history of each record if you want or delete the log record if you are worry about performance.
you can create new field in your db which holds the updated date and time and after every operations in db you may query the db about the recent updates done to your db
You can accomplish this by implementing your own ContentObserver for the URI of the table you want to be notified about. ActiveAndroid has its own content provider you can use or you can implement your own, that works with your tables. In one of my projects I used ActiveAndroid and had my own content provider for 3rd party apps. Upon an update/insert/deletion of a row you should be notified by content provider that something was changed with reference to some URI and if you have a registered content observer to this URI you will be notified.
I have a quick performance question; I have a ListView adapter which populates the listview and I open my sqlite database and close it at the end of populating the view and I populate some information about the list row from the SQLite database. I have noticed a serious performance hit with doing this mainly when scrolling which makes sense. I'm wondering how I can improve the performance of scrolling. I query the database about 3 times.
Database db = new Database(context);
db.open();
viewHolder.first.setText(db.queryFirst());
viewHolder.second.setText(db.querySecond());
viewHolder.third.setText(db.queryThird());
db.close();
Should I keep a reference to DB as an instance variable and just open and close when I query or how should I go about this?
I'm wondering how I can improve the performance of scrolling. I query the database about 3 times.
I recommend querying the database once for the whole adapter. Currently you are sending three queries for each row and none of them are cached. Reading from the drive is a slow process, Cursors are designed to fetch a large amount of data very efficiently and CursorAdapters are designed to use the least resources possible. Using one Cursor and a CursorAdapter will allow users to scroll without noticing any performance loss.
There is also a library called ORMLite which allows you to manipulate database quite easily and you get back a list, which means you load the array first and you don't have any scrolling performance issue. This in combination with lazy loading sections of the database will allow you to seamlessly increase the performance of the scroll.
I have a ContentProvider which handles all the data insertion and retrieval related to my application, I'm following the pattern suggested by Virgil Dobjanschi on Google I/O. I am using the first pattern.
My problem is that I have a logical entity that was represented by multiple tables in the database.
For example, I have an Articles table and an ArticleExtras table. Articles represents the article itself, while ArticleExtras represents addtional information about certain Article, like number of comments.
I used CursorAdapter in the UI to display the Article title and the number of comments of that Article in one row of ListView.
To implement that, I added a left outer join ArticleExtras on statement in my ContentProvider query method for Article table, in order for CursorAdapter to get ArticleExtras along with the Article itself.
When new Articles are fetched from the web, I insert it into the database via ContentProvider, and then the CursorAdapter got notified and update the UI, this part worked as expected.
But when I fetched the number of comments (ArticleExtras), I want the same CursorAdapter, which is watching for changes in the content://com.myapp.content/Articles, to be notified, too, so I can update my row in the ListView.
My current implementation is like this: After inserting ArticleExtras into the database, I start a new query to check if Articles table has any rows that is related to the ArticleExtras I just inserted. If so I'll make a new uri for that Article( for example: content://com.myapp.cotent/Articles/123), and call getContext().getContentResolver().notifyChange(uri, null), so the corresponding CursorAdapter that is watching for changes of this Article will get notified.
Is the approach correct, or is there any better way to implement what I want?
Checkout ContactsProvider2, in it they set the notification uri to the AUTHORITY_URI which appears to be a catch all for the other URIs in the provider. I had the same probem and I have tried this myself for a provider with multiple tables and joins on those tables, and it works fine.
I'm making an application with a database of courses (of school) that the user is participating in. I want to display those courses in a list (im using listview) and use also the subitem (extra information of that course) of the listitems. I found a tutorial (http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/) that displays a list like I want but it uses a class to store the data in instead of a database. So the tutorial uses an arraylist of objects of that class. It would be a detour though if i had to put my information from my database into a class to put it in the list.
Does anyone have a clue how to solve this?
Thanks!
I found a beter way. By using a simple adapter and putting the values of the cursor in a hashmap in a arraylist. Found the idea from this site: http://eureka.ykyuen.info/2010/01/03/android-simple-listview-using-simpleadapter/
Here is a perfect tutorial for what you need. It shows you how to create a simple SQLite database and then populate a listview using the database.
SQLite Database creation and Listview Population Tutorial