how to maintain data consistency in listview - android

I have a app that displays data in a listview using baseadapter. The user is able to click a element in the listview and it takes the user to a page where they can edit the data that is being displayed on that element. When the user clicks back it takes the user to the listview however the changes are not updated. How can i update the listview once the user clicks back.

I think just set your adapter in onResume() method, so each and everytime you should go to on that screen, each and everytime it will refresh your Adapter.

Related

Load list items on demand

I would like to load a list of messages on demand.
I show 20 messages on my listview and when the user scrolls on top of list there is a button to load more 20 messages.
Like whatsapp, when user clicks on the button, more messages are loaded above and the user can scroll 20 more messages.
I have tried to make it with Loaders, every time the button is clicked I publish an event that load messages on the adapter.
However when the adapter is releoaded with new itens the focus goes to end of list.
I have tried to set the focus to the right positon, but the Loader's events reload the adapter and the focus come back to end of list.
The only solution I found was to set the focus after a time, but the user can see the list going to end and coming back to the right postion.
How can I load messages on demand like Whats app?
You have to add only 20 objects in your ArrayList and add HeaderView in your list adapter .On click on headerView .It will load more 20 items in your list adapter . and call notifyDataSetChange(); afterwards.

Dynamically adding place holder list items to ListView AND updating source data set with user input

I have an empty ListView where the user should be able to add each list item manually at the press of a button. When the user clicks on the button, I have a place holder list item appear which contains an EditText. When the user edits the text and it loses focus, I want the source data set to be updated with the users input.
Dynamically add elements to a listView Android
Up to now, I found this answer which uses an ArrayAdapter that connects an ArrayList and my ListView but from my understanding, this only updates the ListView when the ArrayList is updated. To clarify, I want to do the opposite, i.e when the list item is changed within the ListView, I want the appropriate ArrayList (or whatever data source which would make this easier) item to be updated.
I'd appreciate any help with this.
Thanks in advance!
I would suggest storing the position you focus on, then when it loses focus, write the new data to the item in your arraylist by calling getItem(position); followed up by notifyDatasetChanged();

How to call and update a Listview of the current Activity after calling a REST service?

I have a custom listview where each item contains an "Approve" (green) and "Reject" (red) button. When the user clicks on one of the green buttons, the application should call a webservice Rest, responsible for updating the database, ie, "approving" the clicked listview item, and "rejecting" the others listview items. When the user clicks in one of the "reject" buttons, the service will update only the clicked item in db, "rejecting" this item. The service accessed via URL works correctly.
All services I have used in the other screens so far returning the information in a new activity, but this service must update in the same activity, removing all items or only one item. If remove all items, the app must return to the previous activity (because listview is empty). If remove only one item, the app stays in the same activity.
My question is: how to call a rest service (URL) to update a current activity (without going to another activity), and how to remove listView items after that?
Thank you in advance and sorry for bad english
You need to add/remove items from your data model, and then call notifyDataSetChanged() on your adapter. That lets the ListView know to update itself.

Want to clear the listview item permanently?

I want to clera the listview item from the screen when pressing the clear all button. So I used the function m_adapter.clear(); It's clearing the items from the screen. But when I again coming back to the app after exit, those views again coming back. I need to clear them permanently. Can anybody help me in this problem?
Code snippet is as follows:
case R.id.deleteAll:{
m_adapter.clear();
}
If you really want to remove and clear your listview permanently your adapter.clear() will not work.
The actual meaning of this method is to clear your listview and not the listview items.
What you actually have to do is to remove all the elements from the array which you are using to populate the listview.
So this is the only way which will ensure you that your listview will not be loaded with data even when come back to your activity and getting out of it.
If not you will have to call your clear() each and every time you enter your activity, thus making it to look like as if there is not data present.
You'll need to clear the source of your listview data, then refresh the listview. That way, when you reopen the activity the data will not be redisplayed as it won't exist anymore.
Eg. If the listview is populated from a database, delete the data from the database, then refresh the listview.

Listview - list items updated with fetched data in background

Currently i am having listview with multiple items (First time, i fetch data with the use of AsyncTask), now i want to implement the below functionalities:
I am showing Option Dialog(With options Edit, Delete, Read) whenever user do long-click on any item, now if user select "Delete" option, at that time i want to remove item from the listview, listview should be displayed without that item meanwhile the data should also be fetched(in background) from the web.
Anybody knows, how do i implement such?
on the action of delete, delete the data from the source array, and call notifyDataSetChanged() on your adapter. and from the place where you call notifyDataSetChanged, you can do all the web stuff.
Use notifyDataSetChanged() over your Adapter
For display a dialog box when log press of listview item use onContextMenu() method in that onContextmenu u use the listview object and in that contaxt menu u write the code for display a dialog box with 3 options and next one remove item from listview when u click on the delete button in that dialog box is in delete button click listener remove the item from the listview and again set that remaining elements to that listview.

Categories

Resources