0

Problem

I Have a custom activity that load graphics from an XML file, where i have a lot of Buttons, Images and Text.

My Implementation Planning

For implementation i want to use android classes like ImageButton, TextView and ImageView.

I have thinking to use a List<View> for looping all View objects and inflating into a RelativeLayout.

My doubt

Is better a List<View> or List<ImageButton>, List<TextView> and List<ImageView>?

Method implementation in ImageButton or ImageView (Like onClick or some other event), is lost when i convert it to a View object?

Example of code i planned:

ImageButton imageButton = new ImageButton(getContext());

//Implementation of methods and events...
List<View> list = new ArrayList<View>;
list.add(imageButton);
Murdok
  • 116
  • 9
  • 1
    That depends 100% on what you want to do - if you need some method that is only defined in the subclasses then you should split the lists, if you only use methods defined in `View` then use a `List`. – luk2302 Mar 25 '19 at 10:23
  • 1
    The list holds only references of your components. If you create an ImageButton for exmaple, set a click listener and add it to the `List`, nothing will get lost. Only thing is you won't know each view's actual type. – ronginat Mar 25 '19 at 10:28
  • Thanks @ronginat, so is there a method to achieve the true class? – Murdok Mar 25 '19 at 10:32
  • @EpicPandaForce no – Murdok Mar 25 '19 at 10:32
  • When you add a specific type of view to a `List` what you are declaring is "I don't care about the specific type of this view". If that's untrue then don't it. If you find yourself having to cast the item when you retrieve it from the list then that is evidence that you do care about the specific types. – Michael Mar 25 '19 at 10:35
  • But using different lists may not be the right solution, perhaps. Are there other solutions? Maybe an Hashmap `>`? – Murdok Mar 25 '19 at 10:42
  • every time when somebody decides to create a lot of different views manually it means the person doing something wrong...especially if it requires to put all these views in List –  Mar 25 '19 at 10:51
  • Why @Eugene? I can't use the xml hardcoding for my project. – Murdok Mar 25 '19 at 10:59

2 Answers2

1

The list only holds references of your components. If you create an ImageButton for exmaple, set a click listener and add it to the List<View>, nothing will get lost. Only thing is you won't know each view's actual type.

To get the true class of a generic View you can use multiple if statements that check all of your component types, like:

if (view instanceof ImageButton) {
    ImageButton imageButton = (ImageButton)view;
}

instanceof checks if an object is of a specific class or extends it. So make sure you first check for ImageButton before ImageView for example, because its a descendant of the class.

ronginat
  • 1,910
  • 1
  • 12
  • 23
  • First of all, thank you for the answer. Today i will try your solution, i let you know in the day. – Murdok Mar 25 '19 at 10:46
1

You biggest doubt here is

Method implementation in ImageButton or ImageView (Like onClick or some other event), is lost when I convert it to a View object?

NO, this does not happen.

Consider two classes

class Parent{
    void big(){}
}

and

class Child extends Parent{
    void small(){}
}

If you say

Child c = new Child();

then you can use

c.big(); as well as c.small();

but if you say

Parent c = new Child();

you are allowed to use

c.big();

But for calling small() inside Child class you need to cast it

Child ch = (Child)c;
ch.small();

Now if there a number of subclasses, each with different methods available to them which like Child1 with small1() and Child2 with small2() and so on then you can use instanceof for casting

like

if(ch1 instanceof Child1)
{   
    Child1 c1 = (Child1)ch1;
    c1.small1();
}
if(ch2 instanceof Child2)
{
    Child2 c2 = (Child2)ch2;
    c2.small2();
}
Doc
  • 10,831
  • 3
  • 39
  • 63
  • Thank you for the answer. I like a lot your answer because you explain in the details what i doubt. I Want to mark it as correct answer, but i want to ask your opionion if `List `can be the correct way to implements it. – Murdok Mar 25 '19 at 11:10
  • It depends on a number of things. It is good to use the generic form like `List`. Generics allow having a level of abstraction, which would otherwise be lost. – Doc Mar 25 '19 at 11:12
  • Yeah it's a lot of View objects( 100+ ), thanks a lot for the response. Have a nice day! – Murdok Mar 25 '19 at 11:14