0

I have the interface where I have to use buttons not list not anything but buttons. I was wondering if there's a away to group the buttons by ids instead of looping in the switch(which is very long) because of my many buttons. Normally If its a listview adapter I would do something like this if am in an adapteview

 String[] clickitems = getResources().getStringArray(R.array.array) ;
     String toclasses = clickitems[position] ;
             try {
                Class classes = Class.forName(getPackageName()+"."toclasses);
                 Intent intent = new Intent(Main.this, classes);
                 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
                 startActivity(intent) ;
            } catch (ClassNotFoundException e) { 
                e.printStackTrace();
            }
        }

Now i would like to use the same method for getting the clicked button using v.getId() as the array to use instead of the position for that case. Instead of

            switch(v.getId()){
             case R.id.xxx:
               break;
             }

Any ideas on how to approach it that way

0

2 Answers 2

3

You can greatly simplify this by setting the Button's tag to its index in the array. The advantage here is that getting the tag is O(1) instead of O(n), which is what an index lookup is.

When you add the button:

button.setTag(clickitems[position]);

Then your onClick looks like this:

    public void onClick(View v) {
        try {
            Class clazz = Class.forName((String)v.getTag());
            Intent intent = new Intent(Main.this, getPackageName()+"."+clazz);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
            startActivity(intent) ;
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "Couldn't get class from tag", e);
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your contribution so clickitems[position]) could be coming from where while this as i can see it, should be used in the oncreate or wherever you are going to set the onlicklistener? remember all buttons will be using the same on click listener. Thanks
I don't know where or you're creating the Buttons, but that is the place where you'll also need to set the tags.
yeah I get that but the array clickitem[position] comes from where per your example? per mine it i used position when on the adapterview for instance where i can access each item per position but on the onlicklistener how do you get the position
I'm not using the position in the click listener. There's no need since all you really want is the class name, which is the tag value.
Ok i got but it will be long as well when you start to set tag for each button then set clicklistener for each as well, that's a long way. But thanks and I guess it works
1

You can get the position for the button v using:

int position = ((ViewGroup) v.getParent()).indexOfChild(v);

and then use the same method as you shown for an adapterview.

3 Comments

in your example there is only one array.
my bad i typed it wrongly and i could not edit so i deleted the comment. My bad its not two arrays but as you can see its like every button fired the statement that will be executed is `packagename.classname' so this application there is a problem
Thanks it worked I just used it bad i had to create an array of classes and pass it to the String that catches position

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.