ArrayAdapter

Discussion in 'Java' started by Dirk Bruere at NeoPax, Mar 31, 2011.

  1. Dirk Bruere at NeoPax, Mar 31, 2011
    #1
    1. Advertising

  2. Dirk Bruere at NeoPax

    Roedy Green Guest

    On Thu, 31 Mar 2011 02:03:24 +0100, Dirk Bruere at NeoPax
    <> wrote, quoted or indirectly quoted someone who
    said :

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


    You would have to show some code. I have never heard of an
    ArrayAdapter.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    There are only two industries that refer to their customers as "users".
    ~ Edward Tufte
    Roedy Green, Mar 31, 2011
    #2
    1. Advertising

  3. In message <>, Dirk Bruere at NeoPax wrote:

    > 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.
    Lawrence D'Oliveiro, Mar 31, 2011
    #3
  4. On 31/03/2011 03:42, Peter Duniho wrote:
    > On 3/30/11 7:25 PM, Roedy Green wrote:
    >> On Thu, 31 Mar 2011 02:03:24 +0100, Dirk Bruere at NeoPax
    >> <> wrote, quoted or indirectly quoted someone who
    >> said :
    >>
    >>> I have an ArrayAdapter in main.
    >>> How do I access it from another class?

    >>
    >> You would have to show some code. I have never heard of an
    >> ArrayAdapter.

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

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Mar 31, 2011
    #4
  5. On 11-03-30 10:03 PM, Dirk Bruere at NeoPax wrote:
    > 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
    Arved Sandstrom, Mar 31, 2011
    #5
  6. Arved Sandstrom <> wrote:
    > On 11-03-30 10:03 PM, Dirk Bruere at NeoPax wrote:
    >> 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.


    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.
    Andreas Leitgeb, Mar 31, 2011
    #6
  7. On 31/03/2011 11:19, Andreas Leitgeb wrote:
    > Arved Sandstrom<> wrote:
    >> On 11-03-30 10:03 PM, Dirk Bruere at NeoPax wrote:
    >>> 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.

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

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Mar 31, 2011
    #7
  8. Java OOP tutorial (Was: ArrayAdapter)

    Dirk Bruere at NeoPax wrote:

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


    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?

    --
    Michal
    Michal Kleczek, Mar 31, 2011
    #8
  9. Re: Java OOP tutorial (Was: ArrayAdapter)

    On 31/03/2011 12:52, Michal Kleczek wrote:
    > Dirk Bruere at NeoPax wrote:
    >
    >>
    >> 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?
    >>

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

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Mar 31, 2011
    #9
  10. Dirk Bruere at NeoPax

    Nigel Wade Guest

    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.

    --
    Nigel Wade
    Nigel Wade, Mar 31, 2011
    #10
  11. On 31/03/2011 14:47, Nigel Wade wrote:
    > 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, esp since
    other examples like ListView involve multiple casts

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Mar 31, 2011
    #11
  12. Dirk Bruere at NeoPax

    Nigel Wade Guest

    On 31/03/11 15:32, Dirk Bruere at NeoPax wrote:
    > On 31/03/2011 14:47, Nigel Wade wrote:
    >> 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.

    > esp since
    > other examples like ListView involve multiple casts
    >


    ???

    --
    Nigel Wade
    Nigel Wade, Mar 31, 2011
    #12
  13. On 31/03/2011 15:42, Nigel Wade wrote:
    > On 31/03/11 15:32, Dirk Bruere at NeoPax wrote:
    >> On 31/03/2011 14:47, Nigel Wade wrote:
    >>> 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

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Mar 31, 2011
    #13
  14. Dirk Bruere at NeoPax

    markspace Guest

    On 3/31/2011 8:35 AM, Dirk Bruere at NeoPax wrote:

    > 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.
    markspace, Mar 31, 2011
    #14
  15. Dirk Bruere at NeoPax <> wrote:
    > 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.
    Andreas Leitgeb, Mar 31, 2011
    #15
  16. Re: Java OOP tutorial (Was: ArrayAdapter)

    In article <in1ptj$dsv$>,
    Michal Kleczek <> wrote:

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

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Mar 31, 2011
    #16
  17. On 31/03/2011 18:09, Patricia Shanahan wrote:
    > On 3/31/2011 8:35 AM, Dirk Bruere at NeoPax wrote:
    > ...
    >> I get an error: "Syntax error on tokens, misplaced constructs".

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

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Mar 31, 2011
    #17
  18. On 31/03/2011 21:26, Dirk Bruere at NeoPax wrote:
    > On 31/03/2011 18:09, Patricia Shanahan wrote:
    >> On 3/31/2011 8:35 AM, Dirk Bruere at NeoPax wrote:
    >> ...
    >>> I get an error: "Syntax error on tokens, misplaced constructs".

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

    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


    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Mar 31, 2011
    #18
  19. Dirk Bruere at NeoPax

    markspace Guest

    On 3/31/2011 1:50 PM, Patricia Shanahan wrote:

    > 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 code crashes out


    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:

    >> ...//Get titleStr etc


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


    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.
    markspace, Mar 31, 2011
    #19
  20. > 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();
    }
    }

    --
    Dirk

    http://www.neopax.com/technomage/ - My new book - Magick and Technology
    Dirk Bruere at NeoPax, Apr 1, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page