Want to clear the listview item permanently? - android

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.

Related

Android ExpandableListView save input in each item's edit text on button click

I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.

android - Is there a way to cache a View?

I have a complex listview. Every item in this listview has three EditTexts and two Checkboxes. Outside the listview is a Button for adding rows to the listview, one for continue and one to go back.
Continue and Back does both call something like:
setContentView(R.layout.stepX);
The problem:
If you press continue and then go back all items from the listview are empty. Is there a easy way to save the input or hold him in ram?
My current workaround is to create a Class for a listitem and if you continue every itemdata is read and saved to an arraylist. If you go back it read the arraylist and rebuild the listitems.

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.

ListView updating Items without being recreated

I want my ListView to work something like the following:
When I press a button (probably from context-menu), I want the user to be able to select more then one item from ListView (probably using check-boxes), but those check-boxes should not be visible before that.
So, the point is, after the user presses a button (let's say "Delete more items"), the listview, should update itself, and appear on every row of the list, a checkbox should appear (allowing me to select the items ID to pass those to server).
How can I achieve that, without having to recreate the list from zero? (how to setVisibility ON, keeping the other content of the ListView as it is, and not doing another request to server).
PS. If you guys, have another better idea, on achieving the Delete More Items, would be much appreciated!
This is just an idea, haven't tried it myself: you build in a checkbox in your listitem layout. Normally, in the getView of your adapter, you set it invisible with
checkBox.setVisibility(8);
When you want to show them, you set some boolean
showBoxes
of your adapter to true, then in the getView oyu don't hide the checkboxes.
Then
notifyDataSetChanged
on the adapter.
Hope it's clear what I mean.

Unable to refresh the list view in realtime after deleting an item from the list

I am having a list view with text and a button. The contents of the listview(the text) are fetched from the SQLite database. I have implemented the code such that, if anyone clicks the button, the corresponding flag of that list item changes in the database table. My intention would be, as soon as the flag is changed in the database table, the corresponding list item should disappear from the list.
Currently, I'm able to change the flag value for the list item (successfully updating in the corresponding column of the table), but the list item remains in the list view until I re-open the app. The list item then disappears as desired.
Is there a way such that as soon as I click the button, and as the flag value is changed at the background, the list refreshes and show me only other items of the list.?
Your help will be much appreciated. Thanks in advance.
You need to do a requery() after changing anything in data source.
-- Edit --
Just found that requery is deprecating, but anyway, the idea is the same. Query for another new Cursor, and set it in CursorAdapter (if you are using it).
Generally, for a ListView, you have to prepare an adapter for the ListView to provide the content of every item in the ListView.
The basic idea is that in order to refresh the listview, you have to call adapter.notifyDataSetChanged() to request the refreshment.
Query with a new Cursor and set the results in the CursorAdapter
invalidate() the existing list view
Thanks all for your responses. I have tried to do it in android way, but seems to be a bit complex. Hence inorder to refresh the list view after updating the list I have re-called the activity using intents so that I'll get a fresh list view with added or deleted list entries.
Hope I'll find other android way of solutions soon.
getLoaderManager().restartLoader(0, null, this);

Categories

Resources