ArrayAdapter

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

I have an ArrayAdapter in main.
How do I access it from another class?
 
L

Lawrence D'Oliveiro

I have an ArrayAdapter in main.
How do I access it from another class?

If it’s from another class in the same source file, you have access by
default.

If it’s from a different source file, make it a public member.

This is all standard Java stuff, nothing peculiar to Android here.
 
D

Dirk Bruere at NeoPax

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

Yes.
It's 04:49 here and my brain is not working too well.
Either its obvious, or I'm not explaining it correctly.
Tomorrow.
 
A

Arved Sandstrom

I have an ArrayAdapter in main.
How do I access it from another class?
I trust you mean a Main class, not a "main" method.

AHS
--
That's not the recollection that I recall...All this information is
certainly in the hands of the auditor and we certainly await his report
to indicate what he deems has occurred.
-- Halifax, Nova Scotia mayor Peter Kelly, who is currently deeply in
the shit
 
A

Andreas Leitgeb

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.
 
D

Dirk Bruere at NeoPax

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.
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) {
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<String>(this,android.R.layout.simple_expandable_list_item_1,BlinkAPI.videoArrayList);
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?
 
M

Michal Kleczek

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?
 
D

Dirk Bruere at NeoPax

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?

The problem is accessing something located in:
public void onCreate(Bundle savedInstanceState)

I thought I could do it with context.
I can certainly access (say)a ListView defined in onCreate by including
videoLV = (ListView )((Activity) mContext).findViewById(R.id.ListViewVideo);

in public BlinkAPI( Context ctx){}

I do not know a corresponding way to get an adapter
 
N

Nigel Wade

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.
 
D

Dirk Bruere at NeoPax

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.
Yes -I have worked out that bit but do not know what it is, esp since
other examples like ListView involve multiple casts
 
N

Nigel Wade

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.
Yes -I have worked out that bit but do not know what it is,

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.
esp since
other examples like ListView involve multiple casts

???
 
D

Dirk Bruere at NeoPax

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.
Yes -I have worked out that bit but do not know what it is,

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.

I know - but I cannot find out how to do it!

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
 
M

markspace

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


Huh. Isn't that sad.

And yet the error doesn't motivate you enough to produce an SSCCE that
would actually allow us to help you out.

Bummer.

Sorry to be sarcastic, but jumping Jesus H. Fucking Christ on a Pogo
stick people, do you even know enough about code to be able to write a
program that compiles? And maybe even re-produces the error? Just to
be clear: I'm not motivated in any way, shape or form to even attempt to
help you until you can at least manage something close to program that
reproduces the error. Your wasting your time by posting nonsense until
you can at least do that.
 
A

Andreas Leitgeb

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>([...]);
[...]
}
...
}

With an IDE you could probably also right-click on the identifier
and select "Jump to declaration" or something similar.

For Vi and Emacs there's something like a tags file, that enables
similar functionality, provided you created/updated that tags file
e.g. with the utility "ctags-exuberant", and press a hotkey when
the cursor is on the identifier (for VI, that would be <Ctrl-]>).

Finally, you can also grep for the identifier, but that depends on
that you filter out with your eyes the declaration from all the
other lines containing it.

Once you found the declaration, you're a good step ahead towards
solving your problem. If you then post the complete declaration line
and the class or method containing it ... help might be possible.
 
J

John B. Matthews

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?

Excellent question. I recall starting with the Sun/Oracle tutorial:

<http://download.oracle.com/javase/tutorial/java/concepts/>

In contrast, this series offered a broader perspective and greater
depth, IMO:

<http://www.dickbaldwin.com/tocint.htm>
 
D

Dirk Bruere at NeoPax

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

Let's start again with my latest code that also does not work

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);


lvRadio = (ListView)findViewById(R.id.ListViewRadio);
}
...
}

public class BlinkAPI {

private static Context mContext;
static ListView radioLV;


public BlinkAPI( Context ctx)
{
BlinkAPI.mContext = ctx;

radioLV = (ListView )((Activity)
mContext).findViewById(R.id.ListViewRadio);

}

private static void updateRadioTitles( ) {
radioTitleAdapter = (ArrayAdapter<String>)
radioLV.getAdapter();
...//Get titleStr etc
radioTitleAdapter.add(titleStr);
radioTitleAdapter.notifyDataSetChanged();
}

This code crashes out
 
D

Dirk Bruere at NeoPax

Let's start again with my latest code that also does not work
To clarify a bit more

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);


lvRadio = (ListView)findViewById(R.id.ListViewRadio);
}
...
}
//****************************************************************************
//Different file
//****************************************************************************

public class BlinkAPI {

private static Context mContext;
static ListView radioLV;


public BlinkAPI( Context ctx)
{
BlinkAPI.mContext = ctx;

radioLV = (ListView )((Activity)
mContext).findViewById(R.id.ListViewRadio);

}

private static void updateRadioTitles( ) {
radioTitleAdapter = (ArrayAdapter<String>)
radioLV.getAdapter();
...//Get titleStr etc
radioTitleAdapter.add(titleStr);
radioTitleAdapter.notifyDataSetChanged();
}

This code crashes out
 
M

markspace

Either you have serious problems with program structure that would
prevent compilation or you have problems pasting code into articles.

I'm guessing this time it's the latter.

This didn't end with a period which is uncharacteristic for the OP. I
think his newsreader just failed, is all.


To the OP: bits like these below:

won't ever compile. Please remove the ...'s and post code that does
compile, or at least shows what error with the compiler you are actually
having.
 
D

Dirk Bruere at NeoPax

Try to strip out everything you can and still reproduce the problem you
are asking about, and then paste into an article *exactly* the code for
which you want help.

Patricia

OK - here's the revision

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);


ListView lvRadio = (ListView)findViewById(R.id.ListViewRadio);
}
...
}

//Everything below is in a separate file

public class BlinkAPI
{

private static Context mContext;
static ListView radioLV;
static ArrayList<String> radioTitleArrayList = new ArrayList<String>();



public BlinkAPI( Context ctx)
{
BlinkAPI.mContext = ctx;
radioLV = (ListView )((Activity)
mContext).findViewById(R.id.ListViewRadio);

}

private static void updateRadioTitles( )
{
ArrayAdapter<String> radioTitleAdapter = new
ArrayAdapter<String>(mContext,
android.R.layout.simple_expandable_list_item_1,radioTitleArrayList);
...//Get titleStr etc
radioTitleAdapter.add(titleStr);
radioTitleAdapter.notifyDataSetChanged();
}
}
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top