Reputation: 31
i have created an expandable listView and i am able to get the child position of it, what i want to acheive is when i click on the child element i need to be redirected to another activity.
the code that i used is as follows
Adapter.java
private Activity activity;
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public StudentAcademicMenuExpandableAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this.activity=activity;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.student_academic_menu_expandable_list, null);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(activity,childPosition,Toast.LENGTH_SHORT).show();
}
});
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
/*txtListChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(_context, (CharSequence) _listDataChild.get(childPosition).toString(),
Toast.LENGTH_SHORT).show();
}
});*/
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.student_academic_menu_expandable_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
MainActivity.java
listDataHeader = new ArrayList<String>(listDataChild.keySet());
listAdapter = new StudentAcademicMenuExpandableAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
{
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " List Expanded.",
Toast.LENGTH_SHORT).show();
}
});
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener()
{
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
// preparing list data
//listAdapter = new StudentAcademicMenuExpandableAdapter(this, listDataHeader, listDataChild);
// setting list adapter
//expListView.setAdapter(listAdapter);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " -> "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
)
.show();
return false;
}
});
Upvotes: 3
Views: 1927
Reputation: 826
Create the following interface for Adapter Class.
public static interface AdapterOnClickHandler {
void onClick();
}
And then, put the interface in your Adapter Class.
private AdapterOnClickHandler handler;
public void setOnClickHandler(AdapterOnClickHandler handler) {
this.handler = handler;
}
Modify your getChildView method make onClick.
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.student_academic_menu_expandable_list, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
//Add this code in your method.....
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.onClick();
}
});
return convertView;
}
In MainActivity.java you can subscribe the AdapterClick as following
listAdapter.setOnClickHandler(new Adapter.AdapterOnClickHandler() {
@Override
public void onClick(BranchModel branchModel) {
//Do something on click.....
}
});
Upvotes: 4