Custom listview with two buttons in each row

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="" 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

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! :)

-------------Problems Reply------------

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. ListViews 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 Views 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 a ListView. 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.

Category:android Views:2 Time:2011-07-12

Related post

  • Problem with custom Listview and Checkboxes/Buttons 2011-08-16

    i´m in trouble with a custom Listview and (Checkboxes or Button). I follow a guide (the android devolopers´s cookbook) and my custom Listview show correctly. An visisble Error occured when selected Checkboxes are scrolling.(Wrong Checkboexes are chec

  • Custom listview with multiple values in a row 2010-07-22

    im trying to implement a custom listadapter that is set to display only distinct values for each row via the sql query. In each row i want to implement a list of textview values that displays data from another sql query. How can i implement an adapte

  • listview with two buttons and list row click 2012-01-05

    I want to have a list view with each row having two buttons like this --------------------------------------------------------------- [TEXTVIEW] [BUTTON][TV][BUTTON] --------------------------------------------------------------- Here I have a onClic

  • Custom ListView from ITemplate-Edit command not firing from added button 2011-09-07

    I'm really hoping someone can help me on this as I am completely stumped and have been unable to locate a response that fixes my issue. I am setting up a custom ListView, adding a button to it, and the button's event simply will not fire. Any help wo

  • Android CustomHow to get Child Views from Custom ListView enclosed within linearlayout without clicking listview 2011-04-08

    I have a custom listview with two buttons and two textViews enclosed within linearlayout. I want to iterate through the listView without clicking the listView rows. When i do listView.getChildAt(position); position can be any integer from 0 onwards i

  • ListView with tow Buttons 2011-04-26

    Hello I am trying to make a listView with tow buttons on each row.I have an adapter where the buttons and the rest views of my list are declared. My problem is that I can not fire the buttons from my main activity.I thought the code below should work

  • android Custom ListView selected row on button click 2011-01-14

    I have custom listview with textview and button. I set the button click handle in the xml files and the implementation in the ListActivity. The problem is when i click the button, I cant get which selected row that i click. Regards --------------Solu

  • Android How do I add some floating button over my custom ListView? 2011-03-25

    I have done my custom ListView, the xml was following: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:

  • Android - ListView dynamic Buttons for each row calling dynamic listeners 2011-05-21

    I'm new to android, i've spent the last 2 days trying previous examples and online solutions but I just can't seem to get my head around it :( I'm able to display a list view, parse some json from online and store a book title, book description and b

  • Get index if checked rows in this customized listView? 2011-08-05

    Hi i am using the following code to generate a custom listview with can i get the index of the checked rows when the user clicks on the button? package com.CustomListView; import; import android.os.Bundle; import and

  • Adding button to each row in listview 2011-09-06

    I want to add button to each row of my listview. I created an XML file called row.xml in my layout folder and added two textviews and a button in that file. But when a button is added, I am unable to click the item of listview. I'm only able to click

  • android: how to delete a row from a ListView with a delete button in the row 2011-10-20

    I know there are lots of threads with more or less same topic but none of them covers my situation: I am populating my ListView with a custom CursorAdapter by using my custom rows. I have two states for the ListView: The first state consists of only

  • How to get rowID of custom ListView's selected row's child? 2012-02-28

    I've a custom list view with 4 textViews and 2 buttons,which i am filling up via database, TV NAME | TV EMAIL | TV NO | TV ID(database primary key,set invisible) | ButtonEDIT | ButtonDelete. I successfully got the id from the database,which is set as

  • I can not perform Button click in custom listview with my custom arrayadapter 2014-09-04

    I have search a lot of questions here and already implemented every answer i get from here, but no one has solved this problem. I have 2 buttons in a row of my custom listview and i want to perform on click on them. Here is my custom adapter and xml

  • add button in each row I create in WPF listview 2010-12-04

    how can I create button in each row I add in listview in c# WPF Like what when you connect to wireless network in windows 7 ? --------------Solutions------------- You need to assign the ListView a DataTemplate for it's items. Try this: <ListView I

  • Custom ListView Row, can no longer select rows 2011-01-08

    Here's my ScrollView: middle:{ layout: { top: 36, bottom: 32, left: 0, right: 0 }, backgroundColor: '#ccc', contentView:{ contentBinding: 'Spanish.wordsController.arrangedObjects', selectionBinding: 'Spanish.

  • android custom listview row formatting question 2011-02-09

    I have a listview that I am trying to customize. The issue I am having with eclipse's android plugin is I am not quite sure how to format the text portions and wrap them around my icon. Here is a diagram with what I am trying to do: I need to know, 1

  • aligning views in a custom listview row 2011-02-14

    I have a custom listview row whose XML has one ImageView and three TextViews inside a LinearLayout. I want to align the last two TextViews to the right. Help is appreciated. Thanks. --------------Solutions------------- I had the same problem , and i

  • Qt: Clickable 'buttons' on each row custom painted with QAbstractItemDelegate 2011-03-29

    I'd like to draw 'clickable' icons (or buttons) on each row of a QListView I'm using my own custom "QAbstractItemDelegate" derived class to paint. Those buttons may change with the custom status of the row (i have access to the underlying data struct

Copyright (C), All Rights Reserved.

processed in 0.068 (s). 11 q(s)