• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Measuring how many times a Button is clicked in Android Application

 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!
Thanks in advance
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i clicked in the button the quantity=1,
if the same button was pressed the quantity allowed is incremented,
and if the button was pressed one time the qunatity equal to 1.
i want to create some thing like this :
report.png
[Thumbnail for report.png]
Report
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's the xml file :


I have the method to get the name of product, the price and how i put the button "delete"
My problem is how can i get the quantity, and how can i calculate the total when i added evrey product
I need your help !
 
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using an onClick listener for those buttons, and a variable that increments or decrements depending on which button is clicked it should be pretty easy to keep track. Then you need a TextView for the current number, and you'd call ist setText method each time one of the button listeners is invoked.

If you don't know what an onClick listener is I'd start by reading up on that.

What specific problems are you facing with the layout? It seems to contain quite a few elements already, adding another TextView should not a problem, right?
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, no probelm for adding the text view
I'll do what you told me, and after I'll tell you what my given as result.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b1 is an image button which contains the product .
here i put the code of count !!!

I have created a linear layout that contains the textView:


 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a method to calculate the count :

i use it in OnClickListener !!
i need your help
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help with what? Post the problem description, and your current code.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as I already said, I want to calculate the number of click of a button.
I created this method "countclick" to incremente the nulber of click
And i use it in OnClickListener method :

I create an attribut: int countbutton = 1;
here's the method putLinearCommandeProduct :

The quantity always equal to 1, and when I click on the same button is not incremented !!
test.png
[Thumbnail for test.png]
I want to get the quantity = 2 for the same product
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is in these lines:

"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.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you're right.
But alwyas i have a probelem !!
when i press a product the quantity =1 and when i pressed another time it became 2 but in another ligne
also when i press another product the quantity became 3 or i press this product one time !!
For example here, I click on the product shawarma twice so I want it to appear on a single line quantity = 2
and the other Product I click one time or it shows me the quantity = 3, or it must be = 1!!
test.png
[Thumbnail for test.png]
test
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need a "countbutton" field for each line; if you have just a single one then you won't be able to differentiate between them.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mmmm, ok !!
do you have any suggestion about the code !!
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll let you figure that out, it's not hard compared to some of the other stuff you're tackling.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some suggestion please
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think how this could be done?
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the idea but the making code is not clear up to now !!
the buttons is added dynamically, and how can i specify each button
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the modified method, do you think it is suitable !!

 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some good general advice: questions are ended by a single question mark. What you're posting -ended by double exclamations marks- over and over again looks more like you're ordering us to do something. You may want to change that behavior.
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I do not mean ordering you .
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I do not mean ordering you .
I'm really need your help, i'm stuck
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the advice I gave; start by implementing that.

Ulf Dittmer wrote:You need a "countbutton" field for each line; if you have just a single one then you won't be able to differentiate between them.


Hint: you'll need something like an ArrayList to keep track of multiple counts.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for your reply
This ArrayList where i put it , in the OnClickLIstener method or where ?
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to put it so that all the code that needs to get at it can get at it. To answer that you first need to think about from which code locations you need to get at it. So what options do you have?
(Please don't answer with "I don't know", ShowSomeEffort that you're really thinking this through.)
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:So what options do you have?
is not too clear, what option do you mean ?
Please be patient with me

 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:So what options do you have?


is not too clear, what option do you mean ?
Please be patient with me
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing is to create as you have already said, an array initialized to 1 its size depends on the number of product

then, according to the ID for each product, if it clicked another time the 1 became 2,what's your opinion about this?
I want to find the solution myself , but I need your help because they are new tips for me
thanks in advance
 
Ulf Dittmer
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, i need the ID because it's reference each product
how can i use this HashMap ?
 
Sarra Sakka
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want a result like this, which also the total is calculate when i add every product or reduce when i delete it
test.png
[Thumbnail for test.png]
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic