ArrayAdapter

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

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.

Those dots are just all the other methods and code to generate titleStr.
The code I have just posted loads the title strings into the
ArrayAdapter OK. The problem occurs HERE

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);
String titleStr="hello";
radioTitleAdapter.add(titleStr);
radioLV.setAdapter(radioTitleAdapter); //HERE - the
debugger splashes up "source not found"
radioTitleAdapter.notifyDataSetChanged();
}
}
 
D

Dirk Bruere at NeoPax

Does this class need to do UI stuff?

It needs to load the data into the arrayadapters that display via
ListViews. It has the methods that are called by button pushes
 
L

Lawrence D'Oliveiro

I need to create it in BlinkAPI or I will not be able to add data to the
adapter

But in the code you posted, the ListView that the radioTitleAdapter is
attached to, as well as the radioTitleAdapter itself, are created in the
mainline “controller†class for the Activity. All you have to do is move the
call to the BlinkAPI constructor down to after that, so you can pass the
radioTitleAdapter to it.
 
L

Lawrence D'Oliveiro

It needs to load the data into the arrayadapters that display via
ListViews. It has the methods that are called by button pushes

Does each list item have a number of controls in it?
 
D

Dirk Bruere at NeoPax

I have an ArrayAdapter in main.
How do I access it from another class?
Or even, what is the ???? pls

public class BlinkAPI
{
static SeekBar volSliderSB;
static ArrayAdapter<String> radioTitleAdapter;
public BlinkAPI( Context ctx)
{
BlinkAPI.mContext = ctx;
volSliderSB = (SeekBar )((Activity)
mContext).findViewById(R.id.SeekBarVolume);
radioTitleAdapter = (ArrayAdapter<String>)????
}
}
 
D

Dirk Bruere at NeoPax

What was wrong with passing the ArrayAdapter to the BlinkAPI constructor?

Simply put, I pass the context to the constructor.
However, I do not know how to access the ArrayAdapter using the context.

I know how to do it with the ListView:
radioLV = (ListView )((Activity) mContext).findViewById(R.id.ListViewRadio);

I cannot work out what to use, or what multiple casts are required
 
L

Lawrence D'Oliveiro

Simply put, I pass the context to the constructor.
However, I do not know how to access the ArrayAdapter using the context.

Ah, I see, you have a circularity in that the ArrayAdapter refers to the
BlinkAPI instance, but BlinkAPI needs access to the ArrayAdapter.

It seems to me you need to separate UI code from data-model code to break
the cycle. How about doing this:

* Define a class, say BlinkItem, that contains all the state for a single
item in the list, with no reference to any UI widgets.
* Define a subclass of ArrayAdapter<BlinkItem> which implements the UI for
displaying/manipulating a list of BlinkItems.

How does that sound?
 
D

Dirk Bruere at NeoPax

Ah, I see, you have a circularity in that the ArrayAdapter refers to the
BlinkAPI instance, but BlinkAPI needs access to the ArrayAdapter.

It seems to me you need to separate UI code from data-model code to break
the cycle. How about doing this:

* Define a class, say BlinkItem, that contains all the state for a single
item in the list, with no reference to any UI widgets.
* Define a subclass of ArrayAdapter<BlinkItem> which implements the UI for
displaying/manipulating a list of BlinkItems.

How does that sound?

It's got to be better than my non-working approaches so far.
I will try it.
Another problem - What does passing context to a class mean if every
item and method in it is static?
 
L

Lew

Dirk said:
Another problem - What does passing context to a class mean if every item and
method in it is static?

In the example you showed it's passed to an instance, not a class.

The purpose generally, though I cannot speak to the specific fragment here, of
the idiom is to give the instance access to class-wide information.

This is common in RMI, JNDI, EJBs (based on RMI), JPA, Spring, web apps and
many, many other libraries. They maintain a static context (for
application-wide information, session-wide and other scopes) in which
instances can share information with each other.

This is, in fact, the purpose of static members overall. Basic, fundamental
Java concept.
 
L

Lawrence D'Oliveiro

What does passing context to a class mean if every item and method in it
is static?

You don’t have to pass any arguments to your own method calls that you don’t
need.

If the system is calling you, that’s a different matter. But then you
usually just pass the same arguments to super.whatever, do your own
processing on what you want and ignore the rest.
 
D

Dirk Bruere at NeoPax

You don’t have to pass any arguments to your own method calls that you don’t
need.

If the system is calling you, that’s a different matter. But then you
usually just pass the same arguments to super.whatever, do your own
processing on what you want and ignore the rest.


This bit of code works in that the adapter is set on the listview with
no problems

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;



//@SuppressWarnings("unused")
//final Data data = new Data(currentContext);

ArrayAdapter<String> myAdapter;
ListView lvRadio;

ArrayList<String> radioTitleArrayList = new ArrayList<String>();


lvRadio = (ListView)findViewById(R.id.ListViewRadio);
lvRadio.setClickable(true);
lvRadio.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> lv, View view,
int arg2, long arg3) { BlinkAPI.playNetRadio(arg2); }});
myAdapter = new
ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,radioTitleArrayList);

lvRadio.setAdapter(myAdapter);
}
}

//************************


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;



@SuppressWarnings("unused")
final Data data = new Data(currentContext);

ArrayAdapter<String> myAdapter;
ListView lvRadio;


lvRadio = (ListView)findViewById(R.id.ListViewRadio);
lvRadio.setClickable(true);
lvRadio.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> lv, View view,
int arg2, long arg3) { BlinkAPI.playNetRadio(arg2); }});

}
}

//This bit of code crashes out when the list adapter is set in
setRadioTitleAdapterListView(), which is called from another thread

public class Data
{
static ListView radioLV;
static public ArrayAdapter<String> radioTitleAdapter;
private static Context mContext;

public static ArrayList<String> radioTitleArrayList = new
ArrayList<String>();



public Data( Context ctx)
{
mContext = ctx;

radioLV = (ListView )((Activity)
mContext).findViewById(R.id.ListViewRadio);
radioTitleAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_expandable_list_item_1,radioTitleArrayList);
}

public static void setRadioTitleAdapterListView()
{
radioLV.setAdapter(radioTitleAdapter);
radioTitleAdapter.notifyDataSetChanged();
}

}
//*******************

I can fill radioTitleArrayList with data.
As far as I can tell the context passed in is the same as in onCreate.
Ditto lvRadio is the same as radioLV
It's the action of setting the adapter that causes it to fail
Is there anything I am obviously doing wrong?
 
L

Lawrence D'Oliveiro

//This bit of code crashes out when the list adapter is set in
setRadioTitleAdapterListView(), which is called from another thread

Do you know what the error is? You can run “adb logcat†to monitor the
system log on your phone while it’s connected via USB, and you should see
the full traceback appear there when your code crashes.

That’s how I’ve been doing most of my debugging.
 
D

Dirk Bruere at NeoPax

“adb logcatâ€

Yes - good advice...BUT

WTF? The whole thing just started working????
Time to backup, quit while I'm ahead, and go to bed.
The reason why can be left until tomorrow
 
L

Lawrence D'Oliveiro

WTF? The whole thing just started working????
Time to backup, quit while I'm ahead, and go to bed.
The reason why can be left until tomorrow

I trust you use version control.

Commit early and often. :)
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top