Reputation: 2661
I have activity which populates listview with customadpater. The Adapter inflates a layout which contains(buttons, textview,seekbar...)
I want the buttons click listeners, updating textview and seekbar thumb to be handled in actvity not inside the getView() method of adapter.
my activity and adapter are different classes. I need a employ a proper design here any help please.
Upvotes: 0
Views: 1389
Reputation: 12587
the best design for implementing this is to do what was suggested by @Autocrab and the code to do this is like this:
public class ParentClass implements View.onClickListener {
public void onCreate(Bundle savedInstanceState) {
MyAdapter mAdapter = new MyAdapter(this, list);
// MORE AND MORE AND MORE
}
public class MyAdapter {
onClickListener mListener;
public MyAdapter (ParentClass activity, List<?> list) {
mListener = (onClickListener)activity;
// MORE AND MORE
}
public View getView(...) {
lButton.setOnClickListener(mListener);
}
}
the only problem with code like that is cyclic refrencing that should be avoided in all coast.
there for, you should probably use WeakRefrences.
when using a WeakReference
it's very important to check every time before getting it that it's not null, like this:
public class MyAdapter {
WeakReference<OnClickListener> mListener;
public MyAdapter (ParentClass activity, List<?> list) {
mListener = new WeakReference<OnClickListener>(onClickListener)activity;
// MORE AND MORE
}
public View getView(...) {
lButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mListener.get() != null) {
mListener.get().preformClick(v);
}
}
});
}
}
Upvotes: 1
Reputation: 3757
implement interfaces for button (onclick), seekbar (onprogress) in your activity.
Then pass these interfaces to the adapter's constructor. in method getView()
assign these listeners to views.
Upvotes: 0