I have an app that stores items in a local database, displayed to the user using a ListView, also in the layout is an EditText that can be used to filter the list. All of this works fine, my issue is I would also like to include items returned by a web service (JSON) in this list also. I'm not having an issue parsing the JSON, my issue is simply how do I insert/add the results from the web service to the ListView?
The data for the ListView comes from a cursor handled by a SimpleCursorAdapter, I just can not figure out how to add the items from the JSON results to the Cursor (I don't actually think you can write to a Cursor outside of performing a Query).
If you don't want to insert the JSON results into the local DB, then one approach would be to use an ArrayAdapter to back the ListView. You can then populate the array by inserting results from the cursor and the JSON query.
Hope this helps,
Phil Lello
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)
In my Android app I have an activity with a listview that displays about 4000 items that are stored
locally in a SQLite Database. If I make an edit to an item, how can I get only this change in the listview,
without having to refresh all the item list (which means a new query for 4000 results)? This query slows down the performance.
I would like something like NSFetchedResultsController of ios.
Strategy should be -
As you are editing the contact, if the update is successful you just fetch the latest info from db for this specific contact only.
Update the edited contact object in your adapter's source List/Array's specific position.
Invoke your adapter.notifyDataSetChanged().
you must have an id for each contact in your SQL lite database.
when you edit a contact update that specific record in your database on basis of the id.
if update is successfull means the information you sent to database is stored successfully .
now you can use the same informtion to update your ArrayList/HashMap whatever you are using to populate your listview.
Ex:- suppose you edited 3rd index contact in your listview on successfull update you add like yourarraylist.add(3,contact);
and the fire notifydatasetChanged.
Try these steps if possible:
Try to fetch the data from db, but do it in different thread which won't effect the main UIThread.
Then you can call the adapter.notifyDataSetChanged() on adapter object. It will do the job i hope :).
I want to fill my ListView with data that's going to come from the web in the form of JSON. The list should be theoretically infinite, with the app making requests for more data if scrolled to the bottom.
Should I use a Cursor or an Array(List) adapter to link my online database with my ListView?
More generally, what are the arguments to consider when choosing between cursor and array?
Well I think you should look at ContentProviders. They are more natural to the problem that you are trying to solve. You have to implement your custom Cursor which ContentProvider returns on a query request.
Ref:
http://developer.android.com/guide/topics/providers/content-providers.html
I have a ListView which uses cursor adapter to show the records from database. When a new records is inserted in database ,it works great and shows that entry on top of ListView on requery.
Now I am facing problem when User scroll down the List, background thread call web service and brings old data. In this case when it does requery, old data is also getting appended on top of list which should append old data at the end of list.
What should i need to do differently, to add old data at the end of List rather than top ? do i need to change method of insertion when I am getting this old data ?
I think you need to store a date field in your bd table and make an ORDER BY in the query....
Can someone point me to right direction, how to create an adapter for AutoCompleteTextView, which would be getting data from SQLite DB, using data, which user entered into the text field? I want to use the user-entered data to filter suggestions for autocompletion. I imagine that adapter should always take user-entered data as soon as changes appears and use it for fetching suggestions on-the-fly. Is that possible? So far I've seen many tutorials for autocompletion where static String arrays were used, but never seen them build dynamically. Is it possible to do it automatically or I need always fetch String array myself and pass as ArrayList to adapter on every AutoCompleteTextView change?
You might be looking for CursorAdapter. Use it just like an ArrayAdapter, but instead of feeding it with an ArrayList, provide a database Cursor. Google for CursorAdapter and you should get a lot more example codes.