I have customized expandable list view showing in Navigation Drawer. I have a click listener in Adapter I would like to send click event from adapter to my activity or fragment.
Is this possible if so how do I go about doing it?
Here is a snippet of my adapter:
public class CustomAdapter implements ExpandableListAdapter {
@Override
public View getGroupView(int position, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_item, parent, false);
}
TextView text = (TextView) convertView.findViewById(android.R.id.text1);
text.setText(items.get(position).getName());
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageIndicator);
if (items.get(position).isHasChild() && items.get(position).getSubMenuItems().size() > 0)
imageView.setVisibility(View.VISIBLE);
else
imageView.setVisibility(View.INVISIBLE);
if (isExpanded)
imageView.setImageResource(R.drawable.down);
else
imageView.setImageResource(R.drawable.next);
return convertView;
}
@Override
public View getChildView(final int childPosition, int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.expandable_list, parent, false);
}
ExpandableHeightListView childList = (ExpandableHeightListView) view.findViewById(R.id.childList);
childList.setAdapter(new CustomAdapter(context, items.get(childPosition).getSubMenuItems()));
childList.invalidate();
parent.invalidate();
final ExpandableHeightListView list = childList;
childList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousItem = -1;
@Override
public void onGroupExpand(int groupPosition) {
ArrayList<eMenuItem> tempItems = items.get(childPosition).getSubMenuItems();
if (tempItems.get(groupPosition).getSubMenuItems().size() == 0) {
Toast.makeText(context, "Start Activity", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Clicked on " + tempItems.get(groupPosition).getName(), Toast.LENGTH_SHORT).show();
}
if (groupPosition != previousItem)
list.collapseGroup(previousItem);
previousItem = groupPosition;
list.invalidate();
parent.invalidate();
}
});
return view;
}
}
WITH HELP FROM Jared Burrows - How to create interface between Fragment and adapter?
Added few things to my CustomAdapter and FragmentActivity
public class CustomAdapter implements ExpandableListAdapter {
public CustomAdapter(MainActivity fragmentactivity)
{
try
{
this.mAdapterCallback = ((AdapterCallback) fragmentactivity);
}
catch (ClassCastException e)
{
throw new ClassCastException("Fragment must implement AdapterCallback.");
}
}
public static interface AdapterCallback
{
void onMethodCallback();
}
@Override
public View getGroupView(int position, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.menu_item, parent, false);
}
TextView text = (TextView) convertView.findViewById(android.R.id.text1);
text.setText(items.get(position).getName());
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageIndicator);
if (items.get(position).isHasChild() && items.get(position).getSubMenuItems().size() > 0)
imageView.setVisibility(View.VISIBLE);
else
imageView.setVisibility(View.INVISIBLE);
if (isExpanded)
imageView.setImageResource(R.drawable.down);
else
imageView.setImageResource(R.drawable.next);
return convertView;
}
@Override
public View getChildView(final int childPosition, int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.expandable_list, parent, false);
}
ExpandableHeightListView childList = (ExpandableHeightListView) view.findViewById(R.id.childList);
childList.setAdapter(new CustomAdapter(context, items.get(childPosition).getSubMenuItems()));
childList.invalidate();
parent.invalidate();
final ExpandableHeightListView list = childList;
childList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousItem = -1;
@Override
public void onGroupExpand(int groupPosition) {
ArrayList<eMenuItem> tempItems = items.get(childPosition).getSubMenuItems();
if (tempItems.get(groupPosition).getSubMenuItems().size() == 0) {
Log.e("value","position"+childPosition);
Log.e("value","position"+groupPosition);
Log.e("value","position"+tempItems.get(groupPosition).getName());
mAdapterCallback.onMethodCallback();
} else {
Toast.makeText(context, "Clicked on " + tempItems.get(groupPosition).getName(), Toast.LENGTH_SHORT).show();
}
if (groupPosition != previousItem)
list.collapseGroup(previousItem);
previousItem = groupPosition;
list.invalidate();
parent.invalidate();
}
});
return view;
}
}
AND IN MY MAIN ACTIVITY:
public class MainActivity extends FragmentActivity implements AdapterCallback
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
this.mMyAdapter = new CustomAdapter(this);
}
And then added this outside Oncreate:
public void onMethodCallback()
{
Log.e("Call","VALUE");
}
}
but I am getting the NPE Error still.. This is strange when I don't use the callback I am not getting NPE but when I use the callback I am getting NPE: http://pastebin.com/wc2ByiFz
at com.ylg.link.CustomAdapter$1.onGroupExpand(CustomAdapter.java:149)
? – Karlakarlanif (mAdapterCallback.onMethodCallback != null ) { mAdapterCallback.onMethodCallback(); }
. Please pay closer attention to my example as it works. – Karlakarlan