Je souhaite afficher une liste extensible à 3 niveaux. Je l'ai fait adaptateur, mais je ne sais pas si les valeurs sont popuated correctement beacuse lorsque je clique sur l'élément de groupe, les accidents d'applications et indique l'erreur LogCat:ExpandView Listview à 3 niveaux affiche une erreur dans Android
11-17 14:57:05.327: E/AndroidRuntime(22709): FATAL EXCEPTION: main
11-17 14:57:05.327: E/AndroidRuntime(22709): Process: com.example.pdt, PID: 22709
11-17 14:57:05.327: E/AndroidRuntime(22709): java.lang.ClassCastException: com.example.model.Model_Search_Group cannot be cast to com.example.model.Model_Search_Child
11-17 14:57:05.327: E/AndroidRuntime(22709): at com.example.ListAdapter.ExpListAdapter$SecondLevelAdapter.getGroupView(ExpListAdapter.java:228)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.AbsListView.obtainView(AbsListView.java:2257)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ListView.onMeasure(ListView.java:1175)
11-17 14:57:05.327: E/AndroidRuntime(22709): at com.example.ListAdapter.ExpListAdapter$CustExpListview.onMeasure(ExpListAdapter.java:147)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.view.View.measure(View.java:16525)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ListView.setupChild(ListView.java:1870)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ListView.makeAndAddView(ListView.java:1793)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ListView.fillDown(ListView.java:691)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ListView.fillSpecific(ListView.java:1349)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.ListView.layoutChildren(ListView.java:1608)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.AbsListView.onLayout(AbsListView.java:2089)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.view.View.layout(View.java:14845)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-17 14:57:05.327: E/AndroidRuntime(22709): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
adaptateur est:
public class ExpListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Model_Search_Group> groups;
public ExpListAdapter(Context context, ArrayList<Model_Search_Group> groups) {
super();
this.context = context;
this.groups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Model_Search_Child> chList = groups.get(groupPosition)
.getChildItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
CustExpListview SecondLevelexplv = new CustExpListview(context);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Model_Search_Child> chList = groups.get(groupPosition)
.getChildItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Model_Search_Group group = (Model_Search_Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.activity_search_group_row, null);
}
TextView edtTxtRef = (TextView) convertView
.findViewById(R.id.edtTxtRef);
TextView edtTxtName = (TextView) convertView
.findViewById(R.id.edtTxtName);
TextView edtTxtPrice = (TextView) convertView
.findViewById(R.id.edtTxtPrice);
edtTxtRef.setText(group.getItemRef());
edtTxtName.setText(group.getName());
edtTxtPrice.setText(group.getPrice());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
public class CustExpListview extends ExpandableListView {
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,
MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Model_Search_Child child = (Model_Search_Child) getChild(
groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.activity_search_child_row, null);
TextView edtTxtSize = (TextView) convertView
.findViewById(R.id.edtTxtSize);
edtTxtSize.setText(child.getSize().toString());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Model_Search_Child> chList = groups.get(groupPosition)
.getChildItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Model_Search_Child child = (Model_Search_Child)
getGroup(groupPosition);
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.activity_search_child_row, null);
TextView edtTxtColor = (TextView) convertView
.findViewById(R.id.edtTxtColor);
edtTxtColor.setText(child.getColor().toString());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
}
Dans mon MainActivity:
public void showList() {
try {
if (ExpListItems.size() == 0) {
Toast.makeText(Activity_Search.this, "No Results",
Toast.LENGTH_LONG).show();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new ExpListAdapter(Activity_Search.this,
ExpListItems);
searchList.setAdapter(adapter);
searchList
.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(
ExpandableListView parent, View v,
int groupPosition, int childPosition,
long id) {
v.setBackgroundColor(0);
return true;
}
});
searchList
.setOnItemLongClickListener(new
OnItemLongClickListener() {
@Override
public boolean onItemLongClick(
AdapterView<?> parent, View childView,
int flatPos, long id) {
long packedPosition = searchList
.getExpandableListPosition(flatPos);
// get item ID's
int itemType = ExpandableListView
.getPackedPositionType(packedPosition);
int groupPosition = ExpandableListView
.getPackedPositionGroup(packedPosition);
int childPosition = ExpandableListView
.getPackedPositionChild(packedPosition);
// GROUP-item clicked
// if (itemType ==
//
ExpandableListView.PACKED_POSITION_TYPE_GROUP)
// {
// // ...
// onGroupLongClick(groupPosition);
// }
// CHILD-item clicked
// else
if (itemType ==
ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// ...
String barcode = ExpListItems
.get(groupPosition)
.getChildItems()
.get(childPosition)
.getBarcode();
System.out.println("barcode" + barcode);
Bundle b = new Bundle();
b.putString("Barcode", barcode);
Intent i = new Intent(
Activity_Search.this,
Activity_Sales_Return.class); //gets the intent that called this intent
i.putExtras(b);
setResult(RESULT_OK, i);
finish();
return true;
}
return false;
}
});
}
});
} catch (Exception e) {
e.printStackTrace();
}
}