Setting an array of buttons

Put your problem here if it does not fit any of the other categories.

Setting an array of buttons

Postby khaled450 » Wed Jun 09, 2010 9:12 pm

Hello there,

I'm trying to make a menu page that has many buttons around 40 buttons. I can hard code every single button like this
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         Button A1 = (Button) findViewById(R.id.btn1);
  2.         A1.setOnClickListener(new View.OnClickListener() {
  3.             public void onClick(View arg0) {
  4.             Intent i = new Intent(Scr1.this, Scr2.class);
  5.             int passStr = 1;
  6.             i.putExtra("FETCH", passStr);
  7.            
  8.             startActivity(i);
  9.             }
  10.          });
  11.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it works fine but it is not a good practice.

I tried making an array for the buttons and then a for loop to activate the setOnClickListener for every button but it didn't work!
This is what my code looks like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         Button[] A;
  2.         A = new Button[50];
  3.         A[1] = (Button) findViewById(R.id.btn1);
  4.         A[2] = (Button) findViewById(R.id.btn2);
  5.         A[3] = (Button) findViewById(R.id.btn3);
  6. ...
  7.         A[40] = (Button) findViewById(R.id.btn40);
  8.  
  9.         //LISTINGS BUTTONS
  10.         for(int x = 1; x<41; x++){
  11.                 A[x].setOnClickListener(new View.OnClickListener() {
  12.                 public void onClick(View arg0) {
  13.                 Intent i = new Intent(Scr1.this, Scr2.class);
  14.                 int passStr = x;
  15.                 i.putExtra("FETCH", passStr);
  16.                
  17.                 startActivity(i);
  18.                 }
  19.              });
  20.                
  21.         }
  22.  
  23.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

The result of doing it this way didn't make any sense for me. All the buttons point to the same thing, meaning all the buttons have the same value, so FETCH will always have the same value in Scr2.java

Help please
khaled450
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed May 19, 2010 8:11 am

Top

Re: Setting an array of buttons

Postby anarche » Thu Jun 10, 2010 1:48 am

Can I suggest a neater way of doing this would be to spin the Listener off into its own class/nested class then just put the id of the calling button as the extra into the Intent?
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Setting an array of buttons

Postby khaled450 » Thu Jun 10, 2010 2:08 am

can you please show an example of that
khaled450
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed May 19, 2010 8:11 am


Re: Setting an array of buttons

Postby khaled450 » Thu Jun 10, 2010 2:32 am

anarche wrote:for inspiration

I'm sorry, but what is that link for !!!
khaled450
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed May 19, 2010 8:11 am

Re: Setting an array of buttons

Postby anarche » Thu Jun 10, 2010 3:10 am

forum sarcasm.

There are a heap of tutorials/blogs/forum Q&A on the internet explaining how to implement a Listener class.

With your initial problem, if you wish to continue with that code it looks fine to me, but I will happily look into it when i get home. What is the actual output?

I wouldn't continue with that code tho' because you wind up creating 40 listeners which in an actual app increase loading time inappropriately...
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Re: Setting an array of buttons

Postby khaled450 » Thu Jun 10, 2010 10:02 pm

:)
i tried to find some tutorials that does exactly what i want but i couldn't; an array of buttons with intents. can you please give me a link for one you recommend
khaled450
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed May 19, 2010 8:11 am

Re: Setting an array of buttons

Postby anarche » Thu Jun 10, 2010 10:11 pm

I meant find a tutorial about setting listeners.

I tried your code and it threw up errors as the x variable in the loop should not be accessable to the anonymous listeners. If you have other code that makes it accessable, then they are all giving the same result because the buttons can only be pressed after the loop has finished, and x will always then be 40.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Setting an array of buttons

Postby khaled450 » Thu Jun 10, 2010 10:39 pm

i declared x as private at the beginning of every class (private int x). so it shouldn't be accessible by other classes. I have another question if you don't mind me asking. my .apk size is greater than 30M and I cannot upload it to the market, guessing the size is the issue here. I tried many times to upload it but nothing, i get a (Upload a valid .APK file) error, but when i tried to upload the same project (DEMO), 5M size, it uploads fine. If you know anything about upload limit please tell me 'cause I am thinking of doing another project using different methods than including all my media (images) in my .APK file.

My idea of doing it is to upload all my images on a server and allow the user to download those images to the SD card onclick, if you know what i mean. However, I am not familiar with some ideas
1. Creating a file on the SD card that is accessible by my app.
2. Establishing an active link to download a media to a certain file on the SD card.

and some other privacy issues that I wont bug you with.
khaled450
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed May 19, 2010 8:11 am

Re: Setting an array of buttons

Postby anarche » Fri Jun 11, 2010 12:01 am

if x is declared as private at the beginning of each class, the (int x = 0 ... ) in the loop will clash. also in your loop you should be starting at 0, not 1. If you are accessing the class variable then every button will send the value 40, because that is what you are setting it to.

I know nothing of uploading to the Market, but if you can only guess at why something doesn't upload I can only guess at helping. Have you created your own key?
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 29 guests