This question has been asked a lot of times and I've systematically gone through each and tried to find which version of performItemClick works. Unfortunately I just can't get any of them to work despite my onclick method being called.
I have a listview with 5 items embedded into a fragment. This fragment is controlled by a fragmentstatepageradapter and a viewpager. I'm trying to restore the saved state.
lv.setAdapter(new SimpleAdapter(getActivity(), list,
R.layout.list_imageview, new String[] { "answer" },
new int[] { android.R.id.text1 }));
The simple adapter takes in a custom layout which has a textview and imageview within a layout. Initially the imageview is null and on click it is set to a tick or cross. This works when clicking the items myself just doesn't work programatically as follows:
#Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
for (int i = 0; i < 5; i++) {
lv.performItemClick(lv.getAdapter().getView(i, null, null),
i, i);
Log.e("restoring state", "" + i);
}
}
}
I can confirm that onViewStateRestored is called and onItemClick is also called. I assume it's a problem in the way I'm performing the item click. I am currently just trying to get it work; I know I haven't yet checked which items have been clicked to selectively click them but that's an easy boolean[] away.
I thank you for your help, and sorry for such a simple question that's been repeated quite a few times; despite reading them I am still unable to get them to work.
I assume it's a problem in the way I'm performing the item click.
Right now you call performItemClick giving it a new View created by the getView method of the adapter. The problem is that if you setting the image is somehow related to the row view the onItemClick callback receives, you'll not see any results as that view is not related(it's "in the air") to the row view that is actually seen on the screen(or present in the ListView). But I'm just guessing.
Anyway, you shouldn't tie that ImageView work to the OnItemClickListener, you should implement it at the adapter level(and call notifydataSetChanged()), especially as you're trying to set multiple rows at once.
Related
I have an ArrayAdapter linked to a ListView.
mListView.setAdapter(mArrayAdapter);
Whenever I reset the ArrayList data to the ArrayAdapter:
mArrayAdapter.clear();
mArrayAdapter.addAll(mArrayList);
mArrayAdapter.notifyDataSetChanged()
the ListView gets correctly updated
however, if just after the above three lines, I call my custom method mListView.hasScrollbar() to detect whether the listview has a scrollbar or not, I get a null lastVisibleItem:
public boolean hasScrollbar() {
View lastVisibleItem = (View) getChildAt(getChildCount() - 1);
if (lastVisibleItem.getBottom()>=getHeight()) {
return true;
}
return false;
}
does it mean that the listview is still refreshing?
My main question is:
how can I test if the listview has the scrollbar after resetting the adapter with new data?
thank you for any help!
Using getLastVisiblePosition / getFirstVisiblePosition is a valid method of detecting wether you have scrolling or not within the list view (aslong as you compare it to getCount() and do your math ofc).
The problem you have as you already guess is that you are attempting to check out of sync.
In order to sync your query when the adapter already filled your List Data and updated changes, you need to issue a post request to the list, which will stack that petition to the message queue of the adapter.
yourAdapter.notifyDataSetChanged();
yourAdapter.getListView().post(new Runnable() {
#Override public void run() {
//your code here
}
});
Make sure to call that after notifySetDataChanged() of course. Because you want the list to update before the check.
I think your question equals to "how to tell if list view contains items need to displayed on more than one screen"?
So my suggestion is to use listView.getFirstVisiblePosition() and getLastVisiblePosition() to tell it.
I'm having a bit of trouble preserving the scroll position of a list view when changing it's adapter's data.
What I'm currently doing is to create a custom ArrayAdapter (with an overridden getView method) in the onCreate of a ListFragment, and then assign it to its list:
mListAdapter = new CustomListAdapter(getActivity());
mListAdapter.setNotifyOnChange(false);
setListAdapter(mListAdapter);
Then, when I receive new data from a loader that fetches everything periodically, I do this in its onLoadFinished callback:
mListAdapter.clear();
mListAdapter.addAll(data.items);
mListAdapter.notifyDataSetChanged();
The problem is, calling clear() resets the listview's scroll position. Removing that call preserves the position, but it obviously leaves the old items in the list.
What is the proper way to do this?
As you pointed out yourself, the call to 'clear()' causes the position to be reset to the top.
Fiddling with scroll-position, etc. is a bit of a hack to get this working.
If your CustomListAdapter subclasses from ArrayAdapter, this could be the issue:
The call to clear(), calls 'notifyDataSetChanged()'. You can prevent this:
mListAdapter.setNotifyOnChange(false); // Prevents 'clear()' from clearing/resetting the listview
mListAdapter.clear();
mListAdapter.addAll(data.items);
// note that a call to notifyDataSetChanged() implicitly sets the setNotifyOnChange back to 'true'!
// That's why the call 'setNotifyOnChange(false) should be called first every time (see call before 'clear()').
mListAdapter.notifyDataSetChanged();
I haven't tried this myself, but try it :)
Check out: Maintain/Save/Restore scroll position when returning to a ListView
Use this to save the position in the ListView before you call .clear(), .addAll(), and . notifyDataSetChanged().
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
After updating the ListView adapter, the Listview's items will be changed and then set the new position:
mList.setSelectionFromTop(index, top);
Basically you can save you position and scroll back to it, save the ListView state or the entire application state.
Other helpful links:
Save Position:
How to save and restore ListView position in Android
Save State:
Android ListView y position
Regards,
Please let me know if this helps!
There is one more use-case I came across recently (Android 8.1) - caused by bug in Android code. If I use mouse-wheel to scroll list view - consecutive adapter.notifyDataSetChanged() resets scroll position to zero. Use this workaround until bug gets fixed in Android
listView.onTouchModeChanged(true); // workaround
adapter.notifyDataSetChanged();
More details is here: https://issuetracker.google.com/u/1/issues/130103876
In your Expandable/List Adapter, put this method
public void refresh(List<MyDataClass> dataList) {
mDataList.clear();
mDataList.addAll(events);
notifyDataSetChanged();
}
And from your activity, where you want to update the list, put this code
if (mDataListView.getAdapter() == null) {
MyDataAdapter myDataAdapter = new MyDataAdapter(mContext, dataList);
mDataListView.setAdapter(myDataAdapter);
} else {
((MyDataAdapter)mDataListView.getAdapter()).refresh(dataList);
}
In case of Expandable List View, you will use
mDataListView.getExpandableListAdapter() instead of
mDataListView.getAdapter()
I have a main activity that uses a FrameLayout with the screen split into left navigation and right content panels. The left panel is used for navigation to load different layouts into the right panel (called contentLayout) which can have multiple layouts glued together. I have a ListView in the left navigation panel with onItemClickListeners. In their separate onItemClick methods, I use contentLayout.removeAllViews() and then inflate their layouts like so:
private final OnItemClickListener redListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
contentLayout.removeAllViews();
if (myFirstClassLayout == null) {
myFirstClassLayout = FirstClassLayout.getLayout(
layoutInflater,
MyActivity.this,
resources,
new ArrayList<String>(),
new ArrayList<Desc>()
);
}
CommonClass.updateListeners();
if (FirstClassLayout != null) contentLayout.addView(myFirstClassLayout);
}
};
private final OnItemClickListener blueListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
contentLayout.removeAllViews();
if (mySecondClassLayout== null) {
mySecondClassLayout = SecondClassLayout.getLayout(
layoutInflater,
MyActivity.this,
resources,
new ArrayList<String>(),
textList,
new ArrayList<Desc>(),
hash
);
}
CommonClass.updateListeners();
if (SecondClassLayout != null) contentLayout.addView(mySecondClassLayout);
}
};
In both the FirstClassLayout and SecondClassLayout, I inflate a ThirdClassLayout which contains a dynamic ListView (let's call it commonListView) that makes use of an ArrayAdapter<String>. The ListView is a listChoiceIndicatorSingle style view, meaning that it shows a radio-button-like icon to the right of each list item.
When the app starts up, the right content panel is empty. If I trigger the redListener or the blueListener, it will display the commonListView in the right content panel with none of the list items selected. If I trigger red first, then trigger blue, then make a selection in the commonListView, the change is reflected back to the FirstClassLayout which is great! However, when I continue by selecting something else in the commonListView from that FirstClassLayout, and then trigger the blueListener to go to the SecondClassLayout, the commonListView in the SecondClassLayout does not reflect the selection I just made. Instead, it is stuck on item I selected when I was last on the SecondClassLayout.
This happens if I perform the testing in the reverse order, so depending on which listener I trigger last, that last listener and layout will have proper UI synching control over both layouts. The one I trigger first seems to be able to only control itself. I need for both listeners to be able to display the exact same ListView selected item on both Layouts at any given time.
The synching of the commonListView from last layout to first layout is achieved through CommonClass.updateListeners() which does:
adapter.clear();
for (String title: statusSet) adapter.add(title);
adapter.notifyDataSetChanged();
commonListView.setItemChecked(positionOfItemChecked, true);
Log.i(TAG, "position checked = " + positionOfItemChecked);
adapter.notifyDataSetChanged();
Since this works from last layout triggered to first, I figure this must be related to a UI refresh problem, but I've searched everywhere for a good solution and tried all suggestions. The Log.i statement prints out correctly in the LogCat for first or last layout triggered, so I know it is hitting the code properly. I just don't understand why the first one is unable to control the last one. I've tried:
commonListView.invalidate();
((BaseAdapter) commonListView.getAdapter()).notifyDataSetChanged();
and AsyncTask and several others, but nothing seemed to work on refreshing the first layout so that it matches the second. Has anyone encountered such an issue? Does anyone know how to fix or get around this?
See if contentLayout.requestLayout() after adding the views fixes your problem.
I figured out what was causing the 2nd one to take hold over the 1st. I inherited a lot of this code and so I'm not familiar with all the parts. Several other pieces were involved. I finally found the portion of code where the original author used a custom AdapterWrapper that implements a custom Listener. The ThirdClassLayout was only creating a new ArrayAdapter and a new AdapterWrapper once by checking to see if the static ArrayAdapter was null. Removing this check allowed both adapter objects to be created new for both layouts. With their own Listeners created, both layouts now respond.
I'm now developing an application that uses a ListView with a
CheckedTextView on every item that managed by an ArrayAdapter to
support multiple chooses. The contents in my ListView are dynamic, that
means, can be changed during runtime. Now I try to use
ListView.getCheckedItemPositions() to get all checked items, Because I want
to save all the checked positions and auto-check them when user go back to
this page again. So I need to save checked results for every page.
For the first page everything works fine as expected. But when user goes to
another page and make some chooses, the result array that ListView returned
contains some positions that are never checked. I don't why ListView has
this strange behavior. Even for a page that in fact no checked happens but
ListView gives me a result that indicates there's one item has been checked.
could anyone who can teach me how to get the position of CheckedTextView
in its OnClickListener callback?
example code is appreciate.
Thanks in advance...
The listview recycles its views so when you go to a different page and then return to the previous page, the listview recalls the getView() function for its views. To make sure that the order of the checked views are not mixed up, create an arraylist that contains the check state of all the views before initializing the adapter. Then pass the arraylist as an argument for the adapter's constructor. There, in the getView() function, set the checked state of each checkable textview based on the arraylist. Then, return to the activity class and override the onItemClick() event. Using the view that is given to you when the function is called, do the following to get the checkable textview and set its checked state:
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View selectedView, int position , long id)
{
CheckedTextView tv = (CheckedTextView)selectedView.findViewById(R.id.textview);
if (tv.isChecked())
{
tv.setChecked(false);
checkStatesOfViews.get(position) = false;
}
else
{
tv.setChecked(true);
checkStatesOfViews.get(position) = true;
}
}
});
I have an unusual issue with my ListView. I currently have a "deselectAll()" method which iterates through the items in my ListView and sets them to unchecked (the items implement the Checkable interface). The "checked" variable gets changed correctly (the view reports as not being checked), but the visual indicator (in this case, a background change) does not show the view as unchecked (the background stays the color of a checked item).
I am iterating and deselecting through my listview like so (I also added my declerations):
private ListView vw_entryList;
private void deselectAll() {
for (int i = 0; i < sAdapter.getCount(); i++) {
((Entry)vw_entryList.getItemAtPosition(i)).setChecked(false);
}
}
The code for my implemented setChecked() is as follows:
public void setChecked(boolean checked) {
_checked = checked;
if (checked) {
setBackgroundResource(R.drawable.listview_checked);
}
else {
setBackgroundResource(R.drawable.listview_unchecked);
}
invalidate();
}
It should be noted that when the items are clicked, they are toggled between checked and unchecked in the OnItemClickListener, and this works ok, with the background change and everything. The code for toggling is very similar:
public void toggle() {
_checked = !_checked;
setBackgroundResource(_checked ?
R.drawable.listview_checked : R.drawable.listview_unchecked);
invalidate();
}
The only difference I can see is where the methods are called from. toggle() is called from within the OnItemClickListener.onClick() method, while my deselectAll() is called from within a button's standard OnClickListener, both in the same class. Does anyone have any ideas as to why the background doesn't change when I call my deselectAll() function?
Do you have custom, non-standard color for the background? If so you might take a look at http://www.curious-creature.org/2008/12/22/why-is-my-list-black-an-android-optimization/ - it boils down to setting android:cacheColorHint attribute of your list to the background color. Maybe that will help.
Edited after further discussion:
I think you need to call getAdapter().notifyDataSetChanged() on the List rather than invalidate(). List is really build in the way that it is relying on adapter to provide the data. What you are doing in fact you have an implicit adapter - Entry is really kept in the adapter and by setting checked, you are changing the data model really, but if you do not call notifyDataSetChanged() the list does not really know that the model has changed and will not recreate the views (invalidate() will only redraw the existing ones).
After trying everything (thanks for your help Jarek), I found a solution that works for my purposes. Instead of implicitly calling the setChecked() within the view that was clicked, I leave it up to the setItemChecked() method within the ListView class.
My updated code:
private void deselectAll() {
for (int i = 0; i < sAdapter.getCount(); i++) {
vw_entryList.setItemChecked(i, false);
}
}
My best guess is that the ListView knows that its items implement the Checkable class, and thus requires itself to be the handler of all item operations. Something along those lines. If anyone can explain in more detail why this solution works while the others did not, I'll reward them with the answer and an upvote.