Settext issue in ViewPager particular version - 5.1.1 android? - android

I am building android application where I am using Custom ViewPager. The Problem is explain below in the following steps:
When I open new activity of ViewPager and click on any button and setText any particular value to it. It's working fine.
Problem occurs when I swipe left-right and then click on button to setText on particular TextView; the value gets set as I can toast the value of TextView using getText. But the value does not change in the UI. For exmaple - I swipe any page and now the TextView contains my current position as 2. If I click button and try to do setText(position++) and toast the value, the incremented value is shown in the toast
But still TextView showing 2.
This issue is coming particular in 5.1.1.

I've also been fighting this one for several hours. Best I can determine ViewPager is really designed for a single view where nothing changes after the first time it appears (which hides the bug where SetView stops working). Think of it as showing fixed pages in a book.
If you have an adapter (in my case a CursorAdapter) in the Fragment, you can get it to update using a notifyDataSetChanged() on the adapter after an update is made. This still doesn't fix the setText being ignored after the first time the page is rendered.
Another solution may be to use Action Tabs with Activities instead of Fragments, but this has been depreciated. Still looking for a better solution.

Related

Edit button in listview item depending on other listview items

I would like to start by saying if you can think of a better title for this problem, feel free to change it since I have no clue how to explain this in a short way.
So here is my problem:
For the application I am trying to make I have these schedules, one schedule for today, and one for upcoming days. Both of these are a listview inside a fragment.
(I used those fragments to create tabs to seperate the two.)
Each game (let's call them games because calling them activities would be confusing) on the schedule has a status, and here is where the annoying part comes. I have been asked to check if any game has been started, and if so I need to disable the buttons to start any other game than the one that is already ongoing.
EDIT: The main problem is that I cannot use findViewById on the listview item because apparently it is a null object reference
I made a little "paint"ing to give you more of a graphical representation.
So long story short, I need a way to check the status inside of every listview item inside of the listview inside of the fragment inside the activity to use said status to disable every button except for the one of the ongoing game.
Little side note: If no games have been started yet, all buttons are enabled.
I hope this made sense.
If you want some code to go with this, feel free to ask but be warned, I am making this inside a testing app so a lot of useless testing data and sloppy code.
EDIT:
This is where I am stuck in a more clear example:
The start buttons are enabled but should be disabled.
Scrolling further down the list, there is a started 'game' and right below it, a game with the same status as in the previous picture where the button is disabled as it should be.
This is because the "isStartable" boolean in my code goes to false after the game with status "start" has passed and the following items are disabled.
When I scroll back up, it is how it should be, the items are all disabled but I need them to be like this when the listview gets filled. Any way to refresh the data in this listview and taking the "isStartable" boolean with it?
This is what it looks like after I scroll back up.
create a model class for your listview data items. Set a boolean variable in model class like isGameStarted. Set that variable as per your result.Then in your listview adapter, put a condition as below
if(isGameStarted){
holder.startButton.setEnable(true);
else
holder.startButton.setEnable(false);

Access Adapter items of Recyclerview

I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.

ViewPager - Hide number of pages

I'm building my Android App using Viewpagers, and I built a ListView that takes 8 seconds to show the full Items.
The page doesn't update the view automatically, thats why the Listview appears empty when it is shown for the first time.
So, I was thinking about creating a new page called loading, and hide the page with the listview, and only show it when the Listview items are already there.
To do that, I would like to know how can I hide pages.
I tried to use an Handler and change the number of pages after 8 seconds, but it didn't work, can you guys give me any idea of how can I do that?
Thanks.
You shouldn't create a new page called loading,it is not a good way to solve it. You can u get data in oncreat first time,then update data by a timeror handler+timertask and so on.

Adapter "holding on" to view when I animate it

I'm using a custom adapter for a GridView, and feeding it an ArrayList of my custom data class.
The idea is, when the user presses (+) on an item, its number goes up, and when they press (-), it goes down.
This has worked perfect with the following code:
Activity:
//playerLayouts is the ArrayList of my Player class.
playerLayouts.get(position).changeLife(-1); //All this does is decrement life by 1.
adapter.notifyDataSetChanged();
Adapter:
holder.textLife = (TextView) row.findViewById(R.id.player.text_life);
...
holder.textLife.setText(Integer.toString(player.life));
So this works great. I press plus, it goes up, I press minus, it goes down. All is well.
But, if I want to put a little "-1" popup in the view that shows that 1 has been subtracted, and I animate the view, a problem arises.
...
//textLifeChange is a different TextView in the layout. Also, I'm using NineOldAndroids.
textLifeChange.setText(Integer.toString(player.lifeChange));
animate(holder.textLifeChange).setDuration(2000).alpha(0); //There are a few more animations in addition to this one.
Basically, whenever the animation is playing (which is a full two seconds), the view doesn't do any more updates.
For example, say "life" = 20. I hit (-), and now it says 19, and my little popup shows "-1" and fades out. Then, just ONE second later, I hit (-1) again. Nothing happens. The "life" TextView still says 19, and the popup just keeps playing its animation with no change. So I wait another second, and the animation is finished. NOW if I press (-) again, it updates properly again.
Interestingly, if I log my setText method, I see that it is updating with the correct number, and getView is being called, but it's just.. not updating visibly.
What's wrong?
I daresay that your animation is hogging UI Thread, and thus prevents changes in other parts of interface. This asnwer shoud help you:
Android: Running thread preventing animation from starting

ActionbarSherlock update data on swipe

I cant seem to find smooth solution for next what one would thought would be simple task:
I am using ActionbarSherlock for my application with 2 tab bars. On first tabbar i have simple input form and on second i would like to show inputed data in ListView.
Widgets and views are not problem here.. the only problem i have is next:
I would like for my listiview to be updated when user scroll into it.
How can I make that happen? Until now I had walk around with TextSwitcher and static variable that executed refresh of ListView.
Thank you

Categories

Resources