java - How can I use my global variable inside an Adapter class? Android -
hi i'm trying pass value using global variable
. have created class file extended application
, add on manifest
.
public class myapplication extends application {}
after had created adapter class
extended baseexpandablelistadapter
, i've search on how set , global variable i've created , found
((myapplication) getactivity().getapplication()).setmy_id(my_id);
and able value use this
integer my_id = ((myapplication) getactivity().getapplication()).getmy_id();
in fragments
, can use getmy_id()
method when putting inside baseexpandablelistadapter
, i'm having error in getactivity(). tried using this
still says cannot resolve method getapplication()
, there other way value of global variable.
i'm doing because i'm trying use cursor
listview
. wanted create expandable listview
data database , cursor
have parameter it's where
condition data in global variable used.
the reason why i'm using global variable because use data in different fragments
not static changes value depends on selected item.
thank in advance.
you need have constructor in class extends baseexpandablelistadapter. defined constructor should receive parameters of context type. here example -
private context mcontext; public yourexpandablelistadapter(context context) { mcontext = context; integer my_id = ((myapplication) context.getapplicationcontext()()).getmy_id(); }
now create instance in activity -
yourexpandablelistadapter ob = new yourexpandablelistadapter(this);
this should work.
Comments
Post a Comment