简体   繁体   中英

android - what button is clicked on expandableListView's child

Hello everibody and thanks to read me.

I hope I will express correctly myself because i'm french... Please tell me if you don't understand something.

I have an expandableListActivity, and, with an adapter, I have put childs on it. Every child has two buttons which reduce or increased a number (named quantity). My difficulty is to know, when a button is pressed, what child is concerned. Please note that when a button is pressed, the child concerned is not always the child selected.

I have seen this page http://www.geekmind.net/2009/11/android-custom-list-item-with-nested.html thanks to the Andikki's answer on this page Creating buttons in expandablelistview and I have partially solve the problem.

Indeed, the tag's solution seem to solve the problem, but it not. I have a bug when I quickly click on a button. All work, but if i quickly click on a button, for example 20 times in a minimum of time, the button clicked is not always right detected.

In my case, i whant to increase or decrease a number "quantity" in a child. When i click on the plus button, or the minus button, it works. But when i quickly click on a same button, it happends that the quantity increased or decreased is not the quantity of the good child !

I put you my code as follow, if you want more, please tell me. Here it's the child_row

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/childname"
    android:textStyle="italic"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
android:gravity="center_vertical"
    android:maxWidth="150dip"
    android:singleLine="false"/>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical|right"
        android:gravity="center_vertical|right">

    <ImageView
        android:id="@+id/decrease_quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/minus"
        android:background="@color/transparent"
        android:clickable="true"
        android:onClick="decreaseQuantity"/>

    <TextView
            android:id="@+id/quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"/>

    <ImageView
        android:id="@+id/increase_quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/plus"
        android:background="@color/transparent"
        android:clickable="true"
        android:onClick="increaseQuantity"/>

</LinearLayout>

</LinearLayout>

Here it's an extract of the getChildView method in my adapter :

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    View v = null;

    if (convertView != null){
        v = convertView;
    }
    else{
        v = inflater.inflate(R.layout.child_row, parent, false); 
    }

    Product product = (Product)getChild(groupPosition,childPosition);

    ImageView decreaseQuantityImageView = (ImageView)v.findViewById(R.id.decrease_quantity);
decreaseQuantityImageView.setTag(groupPosition + "/" + childPosition);

TextView quantity = (TextView)v.findViewById(R.id.quantity);
quantity.setText(String.valueOf(product.getQuantity()));

ImageView increaseQuantityImageView = (ImageView)v.findViewById(R.id.increase_quantity);
increaseQuantityImageView.setTag(groupPosition + "/" + childPosition);

// I do that for all ImageView clickable of child   

return v;

}

And here an extract of my activity (extends ExpandableListActivity) with the getTag()

public void increaseQuantity(View v){

    ImageView increaseQuantityImageView = (ImageView)v.findViewById(R.id.increase_quantity);
    String positions = (String) increaseQuantityImageView.getTag();
    String tabOfPositions[] = positions.split("\\/");

    Product product = (Product)this.retailerAdapter.getChild(Integer.parseInt(tabOfPositions[0]), Integer.parseInt(tabOfPositions[1]));
    product.increaseQuantity();

    this.retailerAdapter.notifyDataSetChanged();

}

I'm not sure, but I think that I have solved the problem. For the moment, I don't see the bug anymore.

Here is the "solution" (if somebody can explain me...).

I have deleted the following code :

if (convertView != null){
    v = convertView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM