When following this tutorial
I am getting a couple of errors that I am not sure how to resolve. In the tutorial, it extends ExpandableListActivity but I am unable to do that since I am using a Fragment so I have to extend it instead.
expandableList.setOnChildClickListener(this);
I am also getting an error from:
adapter.setInflater((LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
The error is LayoutInflater cannot be resolved to a variable
If I set a new listener on expandableList I am not sure if that will work because the listener and its methods are already in the MyExpandableAdapter.
Anyway, I am kind of lost here and if anyone could help me figure this out, that would be great!
Thanks to anyone looking at this!
public class MuscleGroupFragment extends Fragment {
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
ExpandableListView expandableList;
public MuscleGroupFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_muscle_group, container, false);
expandableList = (ExpandableListView) rootView.findViewById(R.id.list);
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
adapter.setInflater((LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE), this); //Getting an error on LayoutInflater
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this); //Issue is this line
return rootView;
}
Here is the adapter class
public class MyExpandableAdapter extends BaseExpandableListAdapter{
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.muscle_group_parent, null);
}
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String message = child.get(childPosition);
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(R.layout.muscle_group_children, null);
}
//((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
//((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public void onGroupCollapsed(int groupPosition) {
// TODO Auto-generated method stub
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
// TODO Auto-generated method stub
super.onGroupExpanded(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
0 comments:
Post a Comment