D
Dirk Bruere at NeoPax
I have an ArrayAdapter in main.
How do I access it from another class?
How do I access it from another class?
I have an ArrayAdapter in main.
How do I access it from another class?
I have an ArrayAdapter in main.
How do I access it from another class?
I assume he's referring to this:
http://developer.android.com/reference/android/widget/ArrayAdapter.html
First link on Google and Bing.
That said, it really doesn't matter what the type of the object is. It's
still just going to be a normal object reference. And accessing object
references that exist in one class from another class is always done the
same way: either provide a method or field in the owning class that will
return it, or pass/assign the value of the reference to a method/field
in the accessing class.
Basically, it's one of the first things someone ought to learn when
learning how to program in an OOP language. Classes that can't get
values from the instance of one class to the instance of another usually
aren't that useful.
Pete
I trust you mean a Main class, not a "main" method.I have an ArrayAdapter in main.
How do I access it from another class?
Arved Sandstrom said:I trust you mean a Main class, not a "main" method.
Back online again.Arved Sandstrom said:I trust you mean a Main class, not a "main" method.
Now, that you spell it out like this, it seems more likely, that
an ArrayAdapter-instance is stored in a local variable of public
static void main(String[] args).
Unless main passes the reference to another class, I doubt
there'd be a way for that other class to access the reference.
Perhaps, reflection can still do it, but I wouldn't bet on it.
Dirk said:public class controller extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context currentContext = this;
final BlinkAPI blinkAPI = new BlinkAPI(currentContext);
lvVideo = (ListView)findViewById(R.id.ListViewVideo);
radioTitleAdapter = new
ArrayAdapter said:lvVideo.setAdapter(radioTitleAdapter);
}
...
}
//*******************************************
public class BlinkAPI {
private static Context mContext;
static ListView albumsLV, videoLV, tracksLV, artistsLV, radioLV;
public BlinkAPI( Context ctx)
{
BlinkAPI.mContext = ctx;
radioLV = (ListView )((Activity)
mContext).findViewById(R.id.ListViewRadio);
}
private static void updateRadioTitles( ) {
...
radioTitleAdapter.add(titleStr); //PROBLEM
radioTitleAdapter.notifyDataSetChanged(); //PROBLEM
}
How do I get at radioTitleAdapter?
There are so many issues with the code above that answering your direct
question does not make sense IMHO (but surely someone is going to answer it
and additionaly point out the issues).
So in hope to be bright and suggest you to search Google for an OOP tutorial
(possibly in Java) - I actually did:
http://www.google.pl/search?q=java+object+oriented+programmin+tutorial
And you know what - nothing really useful! A lot of material covering
(somewhat like in an encyclopedia) what an object/class/instance
variable/method is but nothing really _introductory_ that would illustrate
"live objects communicating with each other to achieve a goal".
Does anybody know of anything like this online?
private static void updateRadioTitles( ) {
...
radioTitleAdapter.add(titleStr); //PROBLEM
radioTitleAdapter.notifyDataSetChanged(); //PROBLEM
}
How do I get at radioTitleAdapter?
Yes -I have worked out that bit but do not know what it is, esp sinceprivate static void updateRadioTitles( ) {
...
radioTitleAdapter.add(titleStr); //PROBLEM
radioTitleAdapter.notifyDataSetChanged(); //PROBLEM
}
How do I get at radioTitleAdapter?
You ask whatever object knows where it is to let you have a copy of a
reference to it.
Since your code doesn't actually show that information I can't give
specific answers. But presumably some instance of class controller [sic]
knows where one can be found, since the onCreate() method of that class
creates one.
Yes -I have worked out that bit but do not know what it is,private static void updateRadioTitles( ) {
...
radioTitleAdapter.add(titleStr); //PROBLEM
radioTitleAdapter.notifyDataSetChanged(); //PROBLEM
}
How do I get at radioTitleAdapter?
You ask whatever object knows where it is to let you have a copy of a
reference to it.
Since your code doesn't actually show that information I can't give
specific answers. But presumably some instance of class controller [sic]
knows where one can be found, since the onCreate() method of that class
creates one.
esp since
other examples like ListView involve multiple casts
Yes -I have worked out that bit but do not know what it is,On 31/03/11 12:21, Dirk Bruere at NeoPax wrote:
private static void updateRadioTitles( ) {
...
radioTitleAdapter.add(titleStr); //PROBLEM
radioTitleAdapter.notifyDataSetChanged(); //PROBLEM
}
How do I get at radioTitleAdapter?
You ask whatever object knows where it is to let you have a copy of a
reference to it.
Since your code doesn't actually show that information I can't give
specific answers. But presumably some instance of class controller [sic]
knows where one can be found, since the onCreate() method of that class
creates one.
What what is?
Some instance of the "controller" class is creating that ArrayAdaptor.
So that instance knows what it created. Ask that instance for a copy of
the reference to the ArrayAdaptor it created. Or get it to act as proxy
and do the work for you by adding a method to that class which performs
the necessary operations on its local copy of the reference.
If I try to add this:
ArrayAdapter<String> getArrayAdapter(){
return radioTitleAdapter;
}
I get an error: "Syntax error on tokens, misplaced constructs".
I don't have a clue what it means
Dirk Bruere at NeoPax said:Back online again.
Here is a code sample:
public class controller extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[...]
radioTitleAdapter = new ArrayAdapter<String>([...]);
[...]
}
...
}
Michal Kleczek said:So in hope to be bright and suggest you to search Google for an OOP
tutorial (possibly in Java) - I actually did:
http://www.google.pl/search?q=java+object+oriented+programmin+tutorial
And you know what - nothing really useful! A lot of material covering
(somewhat like in an encyclopedia) what an object/class/instance
variable/method is but nothing really _introductory_ that would
illustrate "live objects communicating with each other to achieve a
goal".
Does anybody know of anything like this online?
On 3/31/2011 8:35 AM, Dirk Bruere at NeoPax wrote:
...
It probably means that you put the construct somewhere where it is not
permitted by the Java syntax. The actual issue, and what you need to do
to fix it, depends on where you put it in your code.
Patricia
To clarify a bit moreLet's start again with my latest code that also does not work
Either you have serious problems with program structure that would
prevent compilation or you have problems pasting code into articles.
are asking about, and then paste into an article *exactly* the code for
which you want help.
Patricia
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.