0

I'm trying to create a game. I need a kitten object with a image displayed. I need to use this object within multiple activities so I decided it's probably better using an outside class.

I used something like this. But the function findViewById(int) is not aviable from outside an activity. How to do this?

public class Kitten {
    Context parent;
    View parentView;
    ImageView imgBody, imgHead;

    Kitten(Context context, View view) {
        parent = context;
        parentView = view;

        //Create body & head
        imgBody = new ImageView(parent);
        imgBody.setImageResource(R.mipmap.img_kitty_body_white);
        addImage(imgBody);
        imgHead = new ImageView(parent);
        imgHead.setImageResource(R.mipmap.img_kitty_head_white);
        addImage(imgHead);
    }

    private void addImage(ImageView img) {
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.adoptView);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW, R.id.adoptView);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rl.addView(img, lp);
    }
}

The main problem I have, is within the addImage method...

Ledii
  • 261
  • 1
  • 3
  • 13

2 Answers2

0

Have you tried using the method from your specific view? Try parentView.findViewById()

1mike12
  • 2,946
  • 3
  • 27
  • 36
  • Could have sworn I got the same error for trying that a minute ago, but thanks... I guess I use parentView.findViewById(parentView.getId()); EDIT: or possibly just = parentView? – Ledii Sep 30 '15 at 20:26
  • I'm pretty new to android as well, but usually like you said, you can't just use findviewbyid in a non-activity. It has to be used in an already inflated view, and is a child somewhere in that view. When you do it from activity, it's usually because android "knows" through setCotent(someView) see this: http://stackoverflow.com/a/9228826/2532762 – 1mike12 Sep 30 '15 at 20:29
  • Well, I can comfirm that it worked. So thanks again. Also seems to work quicker with this approach than with it as an inbound class for some reason – Ledii Sep 30 '15 at 20:38
0

You should store the image ID in the "Kitten" object and retrieve it where you need with a getter method, or you can pass the parent View if you want to use the findViewById from there.

But, you don't usually store the Views itself.

Alberto Méndez
  • 1,044
  • 14
  • 31
  • A little confused is to say it the least! Android makes no sence to me... The way they do programming seems very complicated! I'm used to c++ where you create a list by saying vector myVector; and not by creating all kinds of crazy builders and content holders and ect... – Ledii Sep 30 '15 at 21:29
  • I removed the "you are a bit confused" part because i don't know if this is common in other languages (i don't think so), but from what i know objects usually store parameters. This parameter can be of any kind but you don't usually store ui elements in objects. You can use Vector here too and it is an object like any other. I am not saying what you was trying wouldn't work, i'm only saying that it isn't the way of doing it – Alberto Méndez Sep 30 '15 at 21:35
  • Instead of having the imageview in your object you could have a list of images ids and create a method to get them from the desired activities, intializing then there, and not in the object itself – Alberto Méndez Sep 30 '15 at 21:37
  • I'm creating these kittens of mine dynamicly. I can't think of a way to store them in any other way that would "simplify" things. – Ledii Sep 30 '15 at 21:52