tags:

views:

1197

answers:

2
A: 

Where do you call addItemsToAdapter()? Can you show us, how you have tried to add simple Strings to your Adapter?

Edit: out of the comments the helpful code sample:

adapter = new ArrayAdapter<Stock>(this, android.R.layout.simple_dropdown_item_1line, stocks);
adapter.notifyDataSetChanged();
textView.setAdapter(adapter);
WarrenFaith
Here's what I did: I defined a Handler and put it in my AsyncTask (called AppTask)params like so: <code>new AppTask().execute(new AppTask.Payload(Consts.taskType.SEARCH_STOCK, new Object[] {text, handler}, this));</code>Then, in my Handler's handleMessage() I called addItemsToAdapter.As for the Strings, I declared this array:private static final String[] COUNTRIES = new String[] { "Belgium", "France", "Italy", "Germany", "Spain" };and instead of calling:myAdapter.add(tmpItem);I called;myAdapter.add(COUNTRIES[i]) and even COUNTRIES[0].Thanks for your help!
Rob
Is there a way I can post the entire code?
Rob
You can upload a minimalistic project zip somewhere or edit your question above and add the code there...
WarrenFaith
Thanks man, I edited the question and added the code
Rob
Hm... I have only one guess: ContentReader.getInstance.getDataItems() is empty... Have you tried to use some Log statements to see some values or better, have you tried to debug your application?
WarrenFaith
The data items are there. During debug I can see that the data items (stock info in this case) structure is populated with the relevant items. I also see each stock being added to Vector stocks...
Rob
hm... the last time I had such an issue with a spinner, I recreated the adapter with the new data (in your case the new stocks): adapter = new ArrayAdapter<Stock>(this, android.R.layout.simple_dropdown_item_1line, stocks);adapter.notifyDataSetChanged();textView.setAdapter(adapter);that helped me...
WarrenFaith
That did it! I really appreciate your help, Warren. But now I can't help but wonder - what's the purpose of adding (either to the adapter itself or its objects) if it cannot be done after binding the adapter... I can simply create the data structure beforehand and provide it as the last argument in the new ArrayAdapter statement...
Rob
if this helped you, mark it as your solution please (the green tick)...
WarrenFaith
+1  A: 

Create an array adapter with a vector or array like:

ArrayAdapter(Context context, int textViewResourceId, T[] objects)

By initializing your arrayadapter, you will make it listen to objects array. Do not add item to the adapter or clear the adapter, do your additions in "objects" array and also clear it. After changes on this array call

adapter.notifyDataSetChanged();

More specifically

ArrayAdapter<YourContentType> yourAdapter = new ArrayAdapter<YourContentType> (this,R.id.OneOfYourTextViews,YourDataList);

yourAdapter.notifyDataSetChanged();
aTextView.setText(yourAdapter.isEmpty()?"List is empty":"I have too many objects:)");

This should be done after loading YourDataList, I checked your code, are you sure handler calls addStockItemsToAdapter() before you look your adapter is empty or not? You should also check if stocks vector has any elements in it.

miette
I did as you suggested but it still doesn't work. I have a Handler whose handleMessage() is called once the data structure arrives. In handleMessage() I call a function which iterates thru the data structure and adds elements to the adapter's T[] objects and then calls adapter.notifyDataSetChanged(). After all this adapter.getCount() still returns zero. What the hell could it be?
Rob
Hi again, i am changing my answer for you. I have tested what you want to do and it works for me.
miette
Hi miette, I appreciate you looking into this. You are suggesting the same as Warren and indeed this solved the problem. Btw, I realize that calling notifyDataSetChanged() is redundant now since the "new ArrayAdapter" statement contains the updated data source. I thought I could have my data source (YourDataList) empty upon creation of the adapter, add items to it along the way and call notifyDataSetChanged() when done adding, but it seems that it doesn't work, does it? Anyway, much obliged my man.
Rob
Actually adding items along the way must have been working. To see this action I can suggest that call YourDataList.clear() and do some additions to YourDataList manually, for example when clicking a dummy button then call notifyDataSetChanged(), it must work in this way.
miette