I have a ScrollView inside my activity, to which Views
are added during runtime. Basically, when the player defines his name and gender (and names and genders of other players) and then clicks the plus button, then three elements are added to a single line inside the ScrollView: one ImageView for the gender (which contains either male or female icon), a TextView to display the player's name and a delete button at the end of the line, so this player can be deleted, for example if the user mistyped the player's name.
If n
is the number of players that are added (or the number the plus button has been clicked), then the number of Views inside the ScrollView is n*3
.
After the plus button to add the player is clicked, I also create a PlayerDraft instance, which contains the name of the player, the player's gender and three integers corresponding to views that are associated with the player (one int for the id of the corresponding icon, one int for the id of the corresponding text view, which represents the player's name and one int for the id of the delete button associated with the player).
I also keep track between the id
s of the delete buttons and this PlayerDraft instances in a hashmap. This way, when a certain delete button is clicked, I can use it's id
to find the corresponding PlayerDraft instance and therefore also the id
s of the Views associated with this player inside the ScrollView.
This is the code that is called each time a certain delete button is clicked (this is basically inside a method which I call on every delete button that is added to set it's on click listener instance ... this code is inside the setOnClickListener
method):
public void onClick(View v)
//delete corresponding player gender icon, player name and the delete button itself
PlayerDraft playerToDelete = idToPlayer.get(v.getId());
if(null != playerToDelete) {
ImageView iconToDelete = findViewById(playerToDelete.getIconViewId());
TextView textToDelete = findViewById(playerToDelete.getNameViewId());
Button b = (Button) v;
Log.d("thc", "Icon id that is to be deleted: " + iconToDelete.getId());
Log.d("thc", "name view id that is to be deleted: " + textToDelete.getId());
Log.d("thc", "delete button id to be deleted: " + b.getId());
player_display_view.removeView(iconToDelete);
player_display_view.removeViewInLayout(textToDelete);
player_display_view.removeViewInLayout(b);
//player_display_view.removeViewAt(0);
//player_display_view.removeAllViews()
Log.d("thc", "Number of elements in player_display_view scrollview: " + player_display_view.getChildCount());
} else {
Log.d("thc", "playerToDelete is null!");
}
}
The problem is that calling player_display_view.removeView(iconToDelete)
or any similar thereof method doesn't work! However, calling player_display_view.removeAllViews()
works.
I've discovered the following problem. The player_display_view
ScrollView always contains only one child and that child is at position 0 in it's array of children. The number of children doesn't change no matter how much views are added to the ScrollView during runtime, which just boggles my ming :/. How can the Views even be displayed inside the ScrollView (they are, testing it on my phone), if they aren't specified as children of that ScrollView? Calling the method player_display_view.removeViewAt(0);
to delete this only child view inside the ScrollView, deletes not only one View of the ScrollView
, but ALL of the added views.
Why is the following happening? Why does the ScrollView contain only one child, despite multiple Views are added to it during runtime? How can I delete specific Views inside the ScrollView?
Thank you for the time taken, I really appreciate it
In general, a ScrollView
contains only one child. This is usually a container (like LinearLayout
or RelativeLayout
) that is scrolled by the ScrollView
. If you are dynamically (programatically) adding View
s to a ScrollView
, it has probably created an internal container Layout
to hold your added views.
You should put a Layout
inside your ScrollView
and manage (add/delete) your View
s inside that Layout
. Then your "delete" code should work.