How to repaint an android ListView without any data change? - android

I met a small problem.
My MainActivity has a Fragment which includes a ListView. There are some items showing on the ListView. After Clicking one item, another Activity will be opened. When I click the "UP" button and return to the MainActivity, everything is displayed properly except the ListView which is disappearing.
I realized that I needed to trigger the ListView repaint.
But so far, I only find one way:
listAdapter.clear();
listAdapter.addAll(...);
It was a little conter-intuitive because the content of the ListView wasn't changed and I just wanted to show them again.
So does anyone know any simple way to implement it?
Thanks!

You could invalidate it by doing
adapter.notifyDataSetChanged()

Related

How can I know if data in the Listview has changed in android?

I have an unsolved problem:
what I want to do: I have an activity with a TextView which shows the mathematical sum of all items: example. The Listview contains several items which have an amount (double). I want to show the sum of all items inside the activity.
Generally, it works fine when the activity runs the first time, but if I add an item to the list (with a button) later on it is shown correctly inside the list. but I have to update the Textview in the activity. how can I do that, because I don't know a function which tells me that da dataset has changed?
what works actually:
the activity, the sum calculation, and the Listview (with custom
adapter).
In the Listview there is a checkbox, when it is changed a dialog is opened where the new amount is inserted. (that is all done in the adapter)
everything is correctly shown in the listview after a change
What should be solved:
Now, as soon as the dialog box is closed, the sum in the activity (which is outside of the Listview) has to be updated. but how do I get that information back to my activity?
Thank you for your support!
Best regards
Jason
The ListView is in the mainactivity and works fine. In the custom Adapter I can change some date of the Item (each line). Now the Data and ListView is updated and correctly shown. But not The TextView in the mainactivity.
I solved it now by passing the TextView object to the Custom Adapter and do the setText(....) there

Android Grid View onItemClick not working

I have a grid view when user presses a button i am changing grid view data and setting adapter again now on item click is not working but Touch listener is working WHY ?
I have two types of adapters when user presses button a i am setting first adapter when user presses button b i am setting second adapter data is changing but on item click not working?
Try to use AsyncTask (http://developer.android.com/reference/android/os/AsyncTask.html).
Because this kind of updates is always a problem, you have to do your adapter's update in onProgressUpdate or onPostExecute.
So, when your button is pressed, you execute your AsyncTask.
I know this is old, but maybe somebody else will seek for answers...
Anyway, I had a same issue and wasn't able to figure out why my onItemClick() didn't work. Instead of implementing onItemClick() inside your adapter, try doing it with adapter.setOnItemClickListener(). It worked for me ;)

Listview go to main when i click in item

I have an app that has a ListView and when I click on some item, it adds the item in an array.
But when I click on some item I don't want change the view, then in Android 4.0 the ListViewgoing to first, but in Android 4.2.2 the ListView doesn't change. Why?
I always want the same result, the second option.
Is there any property for this?
Try making use of the getFirstVisiblePosition() and setSelection() methods of the listView. The first gets the first listview item present on the screen and the second moves the listview to the proper index when you comeback to this activity.
Please read the documentation for some more details about these functions.

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.

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.

Categories

Resources