Updating specific ListView items when resuming activity - android

I have 3 activities containing listviews with custom adapters, an item selected at the first one drives the user to the second one, and so on...
At these listviews, some items are highlighted as "new itens" (each time adapter's getView is called, I check at a database if the current item should be highlighted). Once user has reached the 3rd listview, I mark these items as "checked", and want to propagate this change back to the other listviews...
That means, when user comes back, pulling the 2nd and 1st listviews up from the stack, I want the viewed items not to be highlighted anymore.
I've tried this answer on SO, without success. When executing notifyDataSetChanged() from onResume(), my listview simply didn't shows up. And I'd prefer not to use startActivityforResult()...
here's my code to refresh the listview at onResume():
#Override
public void onResume(){
super.onResume();
//I have basically the same code at onCreate()
adapter = new ListingsAdapter(getApplicationContext(), this);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
here's another answer that sounds promising, but I wasn't sure how can I retrieve an specific view from the adapter... I want to check all visible items with adapter.getView(), but it asks for a convertView and a parent ViewGroup and I couldn't get that
Thanks in advance for any hint on this

Why don't you wanna use startActivityforResult()?
You don't need to create a new adapter every time in onResume(). You only need to change the underlying data (set the checked flag for the visited item) and then call adapter.notifyDataSetChanged(). This will trigger the adapters getView() method for all the visible list elements.

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 ListView last clicked item as first

I'm wondering right now how to implement following functionality in best and most reusable way.
Whe then user clicks an item in the ListView, he goes to DetailsActivity, but when he comes back to ListActivity item that he clicked before is shown as first in the list.
I'm wondering should I implement some kind of sorting in listview or in adapter. Which way would be most reusable and generic eg. clicked item is excluded?
You can use the insert method from ArrayAdapter. When coming back from DetailsActivity, delete the clicked item from your adapter using the remove method, then insert the element at the first position. Of course, you also need to call notifyDataSetChanged when you're done.
To identify when you're coming back from DetailsActivity, you could launch it with startActivityForResult and modify the adapter in onActivityResult. More on that here.
The most simplest thing I could suggest for it is
Remove the item from the list and the add the item to the list on calling on activity result.
Set the data to the adapter (may be using a public method in adapter class)
Call notify data set changed.
Get the item position clicked from onItemClicklistener and delete the item at that position in arraylist or array and add it to 0th position. So while calling DetailsActivity instead of writing startActivity(Intenet) use startActivityForResult(Intent) and in OnActivityResult method do the above said operation and call notifydatasetchanged().

how to remove an item from ListView while getView()?

during getView()
I set the layout to each item in the list.
However there are some items I want to remove from my ArrayList and also not draw in the list.
My code goes like:
getView() {
if (condition a)
{
myArrayList.remove(currentIndex);
return;
}
doMoreLayouting();
}
Is this the right way?
Does it do any hurt when you remove an item from the collection during list drawing?
Does it do any hurt when you remove an item from the collection during
list drawing?
Yes it will, the main problem is that first the item in the collection will get deleted but what if the item is already called by the getView and you are deleting it after it is drawn then there is your first problem it wont get deleted, because you need to call notifyDataSetChanged to call the getView all over again to delete it from the listView.
Second is that what if each item in your list need to delete somewhere in your array then it will call an call getView again and again which is not wise in performance of you application.
I would recommend deleting it from the method of your adapter and make sure that you gather all the data that needs to be delete and call notifyDataSetChanged only once for performance wise.

onCreate -> scroll to a certain part of the list?

I have an intent that loads a new activity, I also have some extra intent data to get when the new activity is started. Depending on what button is clicked on the first activity, it should scroll to a certain part of the scroll view on the next activity
It would be easiest to have it scroll down enough so that a certain item is at the top and the first thing read (defined by its ID)
Does anyone know how to do this?
Thanks
Check out the smoothScrollToPosition method in ListView:
http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)
This is what you can call to automatically show your list view to the desired position
ListView.setSelectionFromTop(index, 0);
to find out where to start you must compare the item that you want the list to show at startup from the list and the item that is received from your intent.
if you exactly know the position of a specific item in the listview you can call the method written above directly

Android ListView set two different adapter. ListView is not updated to newly set adapter until I scroll it

I have 2 different adapter for a ListView. Both adapters extends BaseAdapter.
I have 2 Button in the header view of the ListView.
Button 1 call ListView.setAdapter(adapter1);
Button 2 call ListView.setAdapter(adapter2);
The first time I click either one of the button. The ListView update to another adapter view immediately.
However, the second time I click either one of the button. The Button listener will not trigger until I scroll the ListView.
I have tried to put a toast inside the button listener. The toast is only called after I scroll the ListView.
Anyone have any idea what is the problem?
The UI in Android is efficient and will only update when it NEEDS to. In the case of your list the UI only updates when you scroll it because it needs to fetch more items for the list. To force an update you need to call invalidate() on your ListView.

Categories

Resources