Measuring how many times a Button is clicked in Android Application

Measuring how many times a Button is clicked in Android Application

I am trying to create a simple application that counts the number of times a button is pressed, and display this number in the TextView !!
How can i do this !!
<ScrollView       android:layout_width="wrap_content" 
                                             android:layout_height="wrap_content"   
                                               android:layout_weight="2">  
                                     <LinearLayout  
                                         android:layout_marginTop="2dp" 
                                         android:layout_marginBottom="2dp" 
                                         android:layout_marginLeft="2dp" 
                                          android:layout_marginRight="2dp" 
                                         android:layout_width="fill_parent" 
                                         android:layout_height="wrap_content" 
                                         android:layout_weight="9" 
                                         android:weightSum="23" 
                                         android:orientation="horizontal">  
                                           <LinearLayout  
                                                         android:id="@+id/Linearqte" 
                                                         android:layout_marginTop="2dp" 
                                                         android:layout_marginBottom="2dp" 
                                                         android:layout_marginLeft="2dp" 
                                                          android:layout_marginRight="2dp" 
                                                         android:layout_width="wrap_content" 
                                                          android:layout_weight="0.5" 
                                                          android:orientation="vertical" 
                                                         android:layout_height="fill_parent">  
 
                                                         <TextView  
                                                             android:id="@+id/tvquantity" 
                                                             android:layout_width="wrap_content" 
                                                             android:layout_height="wrap_content" 
                                                             android:text="Quantity" />  
 
                                                     </LinearLayout>  
                                                         <LinearLayout  
                                                             android:id="@+id/Linearname" 
                                                         android:layout_marginTop="2dp" 
                                                         android:layout_marginBottom="2dp" 
                                                         android:layout_marginLeft="2dp" 
                                                          android:layout_marginRight="2dp" 
                                                         android:layout_width="wrap_content" 
                                                         android:layout_height="fill_parent" 
                                                         android:layout_weight="12" 
                                                         android:orientation="vertical" 
                                                         android:layout_gravity="center" >  
 
                                                             <TextView  
                                                                 android:id="@+id/tvproduct" 
                                                                 android:layout_width="wrap_content" 
                                                                 android:layout_height="wrap_content" 
                                                                 android:text="Product" 
                                                                 android:layout_gravity="center"   
                                                                 />  
 
                                                     </LinearLayout>  
                                                     <LinearLayout  
                                                         android:id="@+id/Linearprix" 
                                                         android:layout_marginTop="2dp" 
                                                         android:layout_marginBottom="2dp" 
                                                         android:layout_marginLeft="2dp" 
                                                          android:layout_marginRight="2dp" 
                                                         android:layout_width="wrap_content" 
                                                          android:layout_weight="4.5" 
                                                          android:orientation="vertical" 
                                                         android:layout_height="fill_parent">  
 
                                                         <TextView  
                                                             android:id="@+id/tvpu" 
                                                             android:layout_width="wrap_content" 
                                                             android:layout_height="wrap_content" 
                                                             android:text="PU" />  
 
                                                     </LinearLayout>  
                                                             <LinearLayout  
                                                                 android:id="@+id/Linearbutton" 
                                                         android:layout_marginTop="2dp" 
                                                         android:layout_marginBottom="2dp" 
                                                         android:layout_marginLeft="2dp" 
                                                          android:layout_marginRight="2dp" 
                                                          android:orientation="vertical" 
                                                         android:layout_width="wrap_content" 
                                                         android:layout_height="fill_parent" 
                                                         android:layout_weight="6">  
 
                                                                 <TextView  
                                                                     android:id="@+id/tvbutton" 
                                                                     android:layout_width="wrap_content" 
                                                                     android:layout_height="wrap_content" 
                                                                     android:text="Bouton" />  
 
                                                     </LinearLayout>  
                                     </LinearLayout>  
                                     </ScrollView> 
"countButton++" does not have the same semantics as "countButton+1", which is what you want here. Or, alternatively, "countbutton++; return countbutton;" (in which case assigning the result of the method call to countbutton is superfluous).

You should read up on the prefix and postfix ++ and -- operators so you understand what they do.

As to your question: it doesn't matter much whether we think it's suitable; what matters is whether it works - which you know, because you have tried it.

From a quick look at it, I don't see how it solves any of the problems you mentioned, or how it implements anything close to what I mentioned you needed to do.

That looks like a good start. The "id" parameter is not used - is it needed?

I actually think it is, and now that I see the images you posted, I realize that you want to offer the user a away to delete items. That means that an ArrayList won't do, because the index of items can change. What you actually need is a HashMap<Integer, Integer>, where the key is the ID of the product, and the value is the quantity of that item.

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright