Android Activity method from the adapter class
Below the example for how to call activity method from adapter class.
Some time I try to call activity method from base adapter or adapter class but I unable to call this method or I am getting some error. So finally I got the solution .
Below the code
Create Interface Class
public interface RefreshInterface
{
public void refresh();
}
Then implement this interface to your activity class.
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
MyArrayAdapter myAdapter = new MyArrayAdapter(getApplicationContext(),
R.layout.appsview_item, List, new RefreshInterface
() {
@Override
public void refresh() {
//do something here
}
});
}
Call activity method from adapter class
public class MyArrayAdapterextends
ArrayAdapter<Structure> {
RefreshInterface intr;
public MyArrayAdapter(Context
context, int textViewResourceId,
List<App>
objects, RefreshInterface refreshInt) {
super(context,
textViewResourceId, objects);
intr = refreshInt;
}
public View getView(final int position, View
appView, ViewGroup parent) {
//this is your activity method
intr.refresh();
}
}