我创建了一个ExpandableListView但是我遇到了很多错误。有时我得到“平面列表位置是未知类型”。有时候我没有得到这个错误但是当我点击任何组的SwitchCompact子项时,其他组的SwitchCompact子项也被点击了。我的适配器如下:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private String company_name_array[] = { "PO" , "Opet" , "Shell" , "BP" };
private int company_icon_array[] = { R.drawable.po,
R.drawable.opet, R.drawable.shell,
R.drawable.bp };
static public List<String> stationList;
static public List<String> fuelList;
static public boolean density;
private ImageView image1;
private ImageView image2;
private TextView text1;
private TextView text2;
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 ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
stationList = new ArrayList<>();
fuelList = new ArrayList<>();
density = false;
}
public ExpandableListAdapter()
{
}
@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);
ViewHolder holder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item2, null);
/*holder = new ViewHolder();
holder.toogle = (SwitchCompat) convertView.findViewById(R.id.switch2);
holder.toogle.setText(childText);*/
}
/*else
{
holder = new ViewHolder();
holder.toogle = (SwitchCompat) convertView.findViewById(R.id.switch2);
holder.toogle.setText(childText);
}
holder.toogle.setText(childText);*/
SwitchCompat toogle = (SwitchCompat) convertView.findViewById(R.id.switch2);
toogle.setText(childText);
if(groupPosition == 0)
{
/*ImageView image = (ImageView) convertView.findViewById(R.id.company_icon);
TypedArray img = this._context.getResources().obtainTypedArray(R.array.company_icon);
for(int i = 0; i < company_name_array.length; i++)
{
if(company_name_array[i].contains(childText) && childText.contains(company_name_array[i]))
{
image.setBackgroundResource(img.getResourceId(i, -1));
break;
}
}*/
clickToogle(toogle);
}
else if(groupPosition == 1)//il
{
}
else if(groupPosition == 2)//akaryakıt
{
clickToogleFuel(toogle);
}
else if(groupPosition == 3)//yoğunluk
{
clickToogleDensity(toogle);
}
//txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
public static class ViewHolder {
public SwitchCompat toogle;
}
@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.list_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;
}
public List<String> getArrayList()
{
return stationList;
}
}