How can I add items to a listviewadapter in response to a button being clicked?
I have an activity which shows a Spinner (selection of category of items), a listview to show th开发者_运维问答e orders and another listview -articles- which dynamically gets filled with buttons according to the selected category (spinner). When an article button gets clicked, I want to add the article to the orderAdaptor.
How can I get a reference to the list adapter when I'm in the View.OnClickListener?
You'll need to post your code to get decent help with this, but it sounds like you are implementing the View.OnClickListener as an anonymous inner class inside of your Activity.
You can generally always get to the adapter by fully qualifying the reference:
MyActivity.this.mListView.getAdapter();
where MyActivity is a presumed class name that extends Activity and holds a member ListView named mListView.
You can pass it to your onClickListener's constructor and keep it in a member variable on the listener, or you can use setTag
on the view to stash a reference to the adapter in the button and then retrieve it in the onClick listener with getTag
. Or you could make the view listener a non-static inner class on your activity and access the adapter the same way everything else in your activity does (although I'd usually prefer being explicit and keeping the listener static / in a different class).
精彩评论