How can I use an ArrayAdapter from another activity? I tried doing the following in MyListActivity.onCreate():
setListAdapter(SomeOtherActivity.myAdapter);
where myAdapter is defined and initialized in SomeOtherActivity. However, I get an empty list, even though I verified that SomeOtherActivity.myAdapter is fully populated via a call to:
SomeOtherActivity.myAdapter.getCount();
If I define and initialize my own adapter in MyListActivity with setListAdapter(myLocalAdapter), it works. Once I switch it to setListAdapter(SomeOtherActivity.myAdapter), I get an empty list. I debugged it and found that the adapter’s getView() isn’t even called.
Help please? Thanks.
In MainActivity.onCreate()
listIsDone = false;
myList = new ArrayList<ItemInfo>();
init = new Runnable() {
public void run() {
myList = generate(); // generate the list, takes a while
Collections.sort(myList, new CustomComparator()); // sorts the list
myAdapter = new MyInfoAdapter(MainActivity.this, R.layout.row, myList);
synchronized(this) {
listIsDone = true;
notifyAll();
}
}
};
Thread thread = new Thread(null, init, "Background");
thread.start;
In my SubActivity.onCreate()
setListAdapter(MainActivity.myAdapter);
doStuff = new Runnable() {
public void run() {
synchronized(MainActivity.init) {
if (!MainActivity.getListDone()) {
try {
MainActivity.init.wait(); // wait for list/adapter to be initialized
} catch (InterruptedException e) {
}
}
}
}
};
Thread thread = new Thread(null, doStuff, "Background");
thread.start();
I notice I can’t run myAdapter.notifyDataSetChanged() in a Runnable thread(I get a runtime error), but I can do it in a runnable if I run it with runOnUiThread(); I’m guessing all method calls to the adapter needs to be done in the same UI thread?
Please check the accessibility of myAdapter in currentActivity.
It seems you are using the adapter before it get initialized.
@share the code.. for proper solution