Problems with android-pulltorefresh widget - java

I am having a bit of difficulty implementing the android-pulltorefresh widget by Johan Nilsson found at https://github.com/johannilsson/android-pulltorefresh
The problem I am having is after putting the custom listview into my application everything is fine it, but it asks to Tap to Refresh the list view but I need it to be set to pull down to refresh.
The code I am using below is pretty much from the github page and a screenshot of the app can be found below do demonstrate my issue:
PullToRefreshListView lv = (PullToRefreshListView)findViewById(R.id.listView);
lv.setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
GetData getData = new GetData();
getData.execute();
}
I need the Tap to refresh header gone and only to be shown once the listview has been dragged down. I get the feeling I just need to change some sort of flag but I can't find where this would be.

Unfortunately there is no way to work around this. The entire control is built around the idea that the "Pull to Refresh" header is a normal listview item that gets hidden by scrolling the list upward. Unfortunately, when you have a very short list, the list cannot be scrolled upward to hide the first item cause there are not enough items in the list -- so the fallback is to show the first item (the header) as well and have it display "Tap to Refresh".
EDIT: One kludge you may be able to do is insert dummy blank items so the list has enough items to hide the top header list item.

use this code
Hope it works for you.

Related

RecyclerView item deletion not animated properly

I've got a RecyclerView which contains items, obviously. It uses the DefaultAnimator for all its animations.
When deleting an item, the deletion is animated, but not as it should be. The issue is that it seems like the size of the list is reduced by one first, then the clicked item is deleted and afterwards all items below are moved upwards by one. Take a look at this short video to see what I'm talking about.
The code used for the removal of a item is the following:
MainActivity.events.events.remove(listItems.keyAt(0));
notifyItemRemoved(listItems.keyAt(0));
Where MainActivity.events.events contains the data for the items and listItems.keyAt(0) contains the currently selected item.
What I've tried yet (none of this worked):
Made sure there's no other call which interrupts the animation (like notifyDataSetChanged()).
Implemented the above code directly into a onClickListener for the items inside the adapter.
Implemented the data directly into the adapter instead of a different class.
Replaced the position with getAdapterPosition() or a fixed value (i.e. 0)
Used notifyItemRangeRemoved() after notifItemRemoved().
Hint: I've got the animation to work previously, but as of today it doesn't work anymore.
EDIT:
If I remove the actual removal command (i.e. MainActivity.events.events.remove(listItems.keyAt(0));) from the code snippet given above, the animation is played correctly, but the element then isn't actually removed, so this doesn't solve the problem at all.

Highlight and disable Listview items

I have a little Problem with my ListView.
I fill my ListView with an ArrayList<String> per ArrayAdapter<String> with more than 100 items.
Before my dialog will be show i want to highlight and disable some items. I have found the solution with ListView.post(new Runnable() {...});
I highlight my items with lv.getChildAt(2).setBackgroundColor(Color.BLUE);
and disbable an item with lv.getChildAt(3).setEnabled(false);
I do both also in an OnItemClickListener().
Now my problem:
if in the ListView are 11 items visible at runtime, the the highligt- and disable-pattern will repeat every 11 items.
i.e. if i highlight just the 3. item also the 14., 25. ... item will get a blue background.
And if i disable the 4. item also the 15. and 26. and so on is disabled.
If i scroll fast to buttom and back to top other items are highlighted and disabled.
Another problem is, that i can only access the first (11) visible items in the post-runnable. If i try to highlight the 20. item the app will crash with a NullPointerException.
What can i do to prevent the "item-recycling" and to get full access to all items before the Dialog is shown?
I'm not sure i understand what you want to achieve but here are some suggestions for you.
1) Always Recycle, you should never avoid recicling since you maybe run into another problem, you will run out of memory.
2) In your model implement the checkeable interface, so the model should know if an item is selected or not, Not the view only
3) When iterating each element make use of a ViewHolder and then check the model to see if the elment being inflaed it selected or not and use the desired color
Please have a look at this example link, it describes the use of CAB (Contextual Action Bar) but it uses the things i'm mentioning.
I hope it helps you.

Checkbox in ListView with odd checking behaviour

I am new to Java and Andriod development.
I have a view within my app which contains a ListView
I then have a custom Adapter which inherits from an ArrayAdapter
within the view in the getView method I return a row which contains some text and a checkbox
now so far this all works great my list is populated and I can check my items, and all the events seem to fire as expected.
My problem that I am having is that I check the first 3 items and then I notice that the 11th, 12th and 13th items are checked and as I scroll I see that at regular intervals other checkboxes also seem to be checked in the same pattern.
If I check about 10 checkboxes then it will end up checking all the items in the list of about 80...
can anyone explain what I have done wrong?
I dont think any code will help explain this as I do not set the checkstate of the checkbox anywhere, this is all handled itself, so the fact items are being checked is puzzling me.
Thanks in advance
This is happening because Android recycles list items for performance purposes, here you have the solution of this problem:
ListView reusing views when ... I don't want it to
Hope to help :)
This is expected behavior. ListView reuses views in the adapter.
So what you want is to keep a Set of "selected items" and add an item or remove it when the CheckBox is clicked and then in your getView add the following code:
if(mSelected.contains(getItem(position)) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
This way when the row is reused the CheckBox will be the proper status.

Advance ListView next item / setSelectionFromTop not working / Inhibit user scrolling ListView

Good afternoon,
I have a list view that show a list of items...well not all of them but they are all there. Rather than let the user scroll up / down the requirement asks for and up / down buttons. I have everything handled quite well EXCEPT how to advance the ListView. Say I'm looking at item 3 in the list. The user hits next...I want to ListView to advance to item 4. I am keeping track internally of which page they are on for incrementing / decrementing. I was expecting to find something like this but no dice.
ListView.MoveNext();
or
ListView.Move(int);
Per Brayden's suggestion I tried the accepted solution here: Maintain/Save/Restore scroll position when returning to a ListView and it did not APPEAR to scroll the list....i.e. it stayed on the first list item. I also tried setSelection.
Here is my code:
public void btn_NextClick(View vw_Current)
{
//increment page count 1
i_PageTracker ++;
//advance to next record
//int x = lstvw_LiftData.getFirstVisiblePosition();
View v = lstvw_LiftData.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
lstvw_LiftData.setSelectionFromTop(i_PageTracker, top);
//lstvw_LiftData.setSelection(i_PageTracker);
}
Based upon Vladimir's success and my lack thereof I wanted to provide some additional info that I thought irrelevant but....
I've extended the SimpleAedapter and override the getView. I also have a DataBinder to populate TextViews with certain data from the data object.
Interestingly when I set a breakpoint in the getView of the DataAdapter and the setViewValue of the binder at load we hit them as appropriate. However when the next button is clicked we do not hit them which I would expect if the list was advancing to the next position....hmmmm I wonder....I have another Idea....be right back.
I figured it out....but I don't know what to do about it. See not knowing what I am doing I shot myself. I ONLY want to see one item in the list at a time and I don't want the user to be able to scroll. Well what I did to accomplish that was to override getCount in the DataAdapter and return a 1. This SEEMED to be the answer as only one item displayed cool! But then overriding getCount also means there is only one item to display so we never advance...there's nothing to advance to.
So what I want to know is this. How do I keep the user from being able to scroll and force them to use the navigation button. Do I handle the scroll event and return null?
I figured it out....but I don't know what to do about it. See not knowing what I am doing I shot myself. I ONLY want to see one item in the list at a time and I don't want the user to be able to scroll. Well what I did to accomplish that was to override getCount in the DataAdapter and return a 1. This SEEMED to be the answer as only one item displayed cool! But then overriding getCount also means there is only one item to display so we never advance...there's nothing to advance to.
So what I want to know is this. How do I keep the user from being able to scroll and force them to use the navigation button. Do I handle the scroll event and return null?

Updating the UI during a after a button push

I have been thinking about adding a button to a View which when pressed adds another view on the bottom. It would resemble the Clock application that comes preinstalled in which you push a view in form of an alarm which is then added to a list.
Does anyone have any experience with how to implement that?
Have a look at the documentation for ListView, particularly the setAdapter() method. You'll want to have some sort of backing collection most likely as the adapter, such as an ArrayAdapter, to which you can add new items, and have them populate into the list.
Use a listview, populate it.
Then when you press the button, add something to your data, then:
public void notifyDataSetChanged ()
Since: API Level 1 Notifies the attached observers that the underlying
data has been changed and any View reflecting the data set should
refresh itself.

Categories