I have spent literally two days trying to sort this issue. If anyone could help I would be massively appreciative.
What I'm trying to achieve:
Have a ListView, whereby the player can add new entries (players), through a text field (for the player name), and then a submit button. In each field of the ListView, I display the player name, and then two ImageButtons. One with a male symbol, and one with a female symbol. The male symbol is toggled by default, and the user can set the player as being male or female by toggling either the male button or the female button. Finally, once the user moves onto the next screen (a new activity), the application will save the player names and the attached sex to some form of storage and proceed to the next activity.
What I have achieved:
I have a simple array adapter, which upon the player adding a new player name to the list, I run the notifyDataSetChanged() on it. The adapter also is set to use a custom row layout file. Inside the layout file, it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/relativeLayout1" android:layout_marginTop="5dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/playerName"
android:layout_centerVertical="true" android:text="Derp" android:textStyle="bold" android:layout_marginLeft="5dp" android:textSize="22dp" android:textColor="@color/male_blue"></TextView>
<ImageButton android:layout_height="60dp"
android:layout_width="60dp" android:onClick="maleClickHandler"
android:src="@drawable/male_icon_settings" android:id="@+id/buttonA" android:layout_alignParentRight="true" android:layout_marginRight="65dp"></ImageButton>
<ImageButton android:onClick="femaleClickHandler"
android:layout_height="60dp" android:layout_width="60dp" android:id="@+id/buttonB"
android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="@drawable/female_icon_settings"></ImageButton>
</RelativeLayout>
</LinearLayout>
The two buttons on each row reference to methods in the class file. Here is my code for this:
public void maleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton maleButton = (ImageButton) vwParentRow.getChildAt(1);
maleButton.setImageDrawable(getResources().getDrawable(
R.drawable.male_icon_selected));
vwParentRow.refreshDrawableState();
}
public void femaleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton femaleButton = (ImageButton) vwParentRow.getChildAt(2);
femaleButton.setImageDrawable(getResources().getDrawable(
R.drawable.female_icon_selected));
vwParentRow.refreshDrawableState();
}
I haven't yet implemented any inter-connectivity between these two buttons, to allow only one to be active at a time, or to even untoggle one, since I think I might be taking the wrong approach entirely.
The problem:
Upon adding new entries to the list, AFTER toggling one and/or the other male/female buttons, things get really buggy, and the male/female toggled icon might move as it should, along with the attached player string, or more likely, those toggled will stay on that first row (array position 0 of the list), or even move into the second list position, AND copy themselves as being toggled onto the row above.
How you can help...?
I have attached an image below of my screen, from the emulator, to help illustrate my points Screenshot!
I think that I might need to use some form of custom adapter; I have done so much reading around on the subject, but I can't find anything relevant to what I am trying to achieve, so if you could point me in the right direction, or even try and put together the most basic solution to this type of problem, I would be very grateful.
Finally, when I get this working, which form of storage would be best for storing player names, and their sex? I would like the user to be able to keep开发者_开发技巧 the player list after they quit the application and restarted it.
Thanks for any help! :)
You will need to use a Custom Adapter, which in itself should be able to track the male/female flag for each of it's entries.
Your method will not work since the state of the buttons are managed by the getView method of the adapter. Even if you change them by digging through the children, the next time when the getView method is called, it's going to mess up things.
A lot of this depends on how many players you expect to have in your game. If it's a number that would likely fit on one screen (or very close to it), the ListView
is actually unnecessary. ListView
s and adapters aren't really a convenience method as much as they are a tool to improve performance. They only keep in memory what is on the screen and recycle old, already-displayed View
s for new rows when you scroll--this is why some of your button states are being copied to different rows.
There are a couple of ways you could fix this:
You could write a custom adapter yourself as Kumar Bibek suggests. In this adapter, you would want to override the
getView()
method to make sure each button has the correct state each time the method is called.You could also simply use a
ScrollView
populated with a few of your rows manually if you don't have enough data to warrant using aListView
. This way you wouldn't need to worry about your rows being recycled and button states being out of wack.
In addition, you might want to look into using a RadioGroup
for the gender selector (I can't think of a much better use for radio buttons since they are made to be mutually exclusive).
Also, the outer LinearLayout
in your row XML file looks unnecessary.
As far as storage, you could either use an SQLite database or SharedPreferences
. SharedPreferences
requires no setup, but I feel like an SQLite database is more suited to your needs.
精彩评论