2011-06-25 42 views
3

你好everibody,並感謝讀到我。android - 點擊什麼按鈕expandableListView的孩子

我希望我會正確表達自己,因爲我是法國人......請告訴我,如果你不明白的東西。

我有一個expandableListActivity,並使用一個適配器,我已經把孩子放在上面。每個孩子都有兩個減少或增加數字(命名數量)的按鈕。我的困難是知道按下按鈕時,哪個孩子會擔心。請注意,按下按鈕時,相關兒童並不總是被選中的兒童。

我看到這個網頁http://www.geekmind.net/2009/11/android-custom-list-item-with-nested.html感謝Andikki在此頁上的回答Creating buttons in expandablelistview我已經部分地解決了這個問題。

確實,標籤的解決方案似乎解決了問題,但它不是。 我有一個錯誤,當我快速點擊一個按鈕。所有的工作,但如果我快速點擊一個按鈕,例如在最短的時間內20次,點擊的按鈕並不總是正確的檢測。

在我的情況下,我想增加或減少一個孩子的數量「數量」。當我點擊加號按鈕或減號按鈕時,它可以工作。 但是,當我快速點擊一個相同的按鈕,它發生的數量增加或減少不是好孩子的數量!

我把你的代碼如下,如果你想要更多,請告訴我。 這是該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> 

這是在我的適配器的getChildView方法的摘錄:

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; 

} 

在這裏,我的活動的提取物(延伸ExpandableListActivity)與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(); 

} 

回答

1

我不確定,但我認爲我已經解決了這個問題。 目前,我再也看不到這個bug了。

這裏是「解決方案」(如果有人能解釋我......)。

我刪除了下面的代碼:

if (convertView != null){ 
    v = convertView; 
} 
相關問題