0

I have a listview where you can see your friends and in every listview item I have a button where you can follow them. Above the listview I have another button and if you click on it I want it to follow all your friends instantly.

Is there a way where you can click all the buttons from an listview at once?

Laurenswuyts
  • 2,144
  • 4
  • 21
  • 39
  • 1
    You may not have to "click" all the buttons at once. But you can always loop through all the friends and call the code that the button listener will end up calling anyway! – mray190 Jun 24 '15 at 20:40

4 Answers4

2

This is how I would suggest setting up your code:

You should have a custom list adapter where you should have your button listener that gets called when a listview item is clicked. This button listener should call a method that 'checks' this friend as followed, or execute some code to follow that person.

In your activity class (where you set the list adapter to your listview), you can then have a button listener for the master follow button. When this button is clicked, iterate through each of the items in your listview and call the method to 'follow' them.

If you post your list adapter and base code, I can help you set it up. The base of the code can be referenced from: https://stackoverflow.com/a/8166802/1869576

Community
  • 1
  • 1
mray190
  • 496
  • 3
  • 13
  • iterating through the items in his listView will only click the visible buttons, if his list is scrollable, it won't effect buttons yet-to-be-created. – NameSpace Jun 24 '15 at 20:39
  • I don't mean physically click the buttons. Just call the code that the onClick listener is calling anyway. All of the row items are stored in the list adapter regardless of if they are shown on the screen – mray190 Jun 24 '15 at 20:40
0

The problem here is that a ListView recycles Views so you don't really have a "follow" button for all your friends. You will have Views for all the friends you see on the screen and a few off the screen but that is it. If you have a 100 or more friends in that list or even a dozen or so more than you see on the screen, there is no "follow" button.

In your case, you have two options.

  1. Look through your list of friends, get whatever ID you need and make the api call for each friend and when successful notifyDatasetChanged on your ListView to update it.
  2. For each item in your ListView look through it and call getView for each position, get the button you want from there and call performClick on it. While this will work and probably have the desired result, I would not recommend it as it's just a lot of processing for no reason.
Ali
  • 12,354
  • 9
  • 54
  • 83
0

i'm not sure about what all you're using, as far as frameworks, libraries, etc., but with angular, it's actually pretty easy to do something like this. check out this plunker. http://plnkr.co/W3Pm5uOBt9WgVtyIXNky.

it's a simple to-do prototype. note the "mark all" button.

 $scope.markAll = function() {
$scope.todos = $scope.todos.map(function(e) {
  e.done = $scope.allDone;
  return e;
});

};

(html file below)

  <p>
   <input type="checkbox" ng-model="allDone" ng-click="markAll()" />
   Mark All
  </p>
spb
  • 4,049
  • 6
  • 22
  • 30
0

Won't work, not that way.

The listView is a view of only a section of your data. If only 10 items fit on screen, then only 10 buttons exist. Buttons that fall off the screen are "recycled" so that they can be used for the data that is coming into view. ListViews don't read ahead, that's what makes them efficient at displaying large data sets.

Rather cycle the data that backs your listView, instead of trying to cycle actual listView buttons.

NameSpace
  • 10,009
  • 3
  • 39
  • 40