I have a custom list adapter which is inflated in a fragment, and that fragment is embedded on an activity, i want to implement an interface to handle clicks on an image on that adapter from the activity class.. but im getting null pointer exception .. here are my codes:
this is part of my custom adapter class :
public ActionSelection mCallBack;
public interface ActionSelection {
public void onActionSelected(int position);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.main_list_item, null);
// Obtain current mainListItem using position
MainListItem item = mainListItems.get(position);
// Set the custom view
if (item != null) {
((SmartImageView) view.findViewById(R.id.imageview_mainlist_profile_picture)).
setImageUrl(item.getProfile_picture());
((TextView) view.findViewById(R.id.textview_mainlist_fullname)).
setText(item.getFullname());
((TextView) view.findViewById(R.id.textview_mainlist_username)).
setText(item.getUsername());
((ImageView) view.findViewById(R.id.imageview_mainlist_action)).
setImageResource(item.getAction());
ImageView action = (ImageView) (view.findViewById(R.id.imageview_mainlist_action));
action.setId(position);
action.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallBack.onActionSelected(5);
}
});
}
and on my MainActivity i implemented the interface with the following code:
@Override
public void onActionSelected(int position) {
Log.v("test", String.valueOf(position));
}
You never set the mCallBack
variable to anything. You could for example pass it in the constructor of your adapter.