using findviewbyid in a class that does NOT extend Activity in android

前端 未结 4 649
无人及你
无人及你 2020-12-13 06:35

I have a class that is currently extending Activity and I have methods like findViewById, ArrayAdapter etc. I want to turn it into an independent class but all the above met

4条回答
  •  没有蜡笔的小新
    2020-12-13 06:44

    if you want to call any function that belongs to Activity then only thing you need to have is context of the Activity.

    eg.

    class A extends Activity {
        Context ctx;
    
        void onCreate(Bundle b)
            ctx = this;
            B bob = new B(ctx);
        }
    }
    

    Here is class B.

    class B {
    
        B (Activity a) {
            a.anyFunctionOfActivity();
        }
    }
    

提交回复
热议问题