Wednesday, February 5, 2014

ExpandableListAdpater problem



Rewrite public View getGroupView (int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) I return a TextView type variable, why would the following error ah: java.lang.ClassCastException: android.widget.LinearLayout $ LayoutParams cannot be cast to android.widget.AbsListView $ LayoutParams. TextView is a subclass of View, returning subclass no problem in syntax, ah, how it happened to forget everyone pointing.
Reply:
The View is a need to return convertView Okay ......
Reply:
Here you can not directly return textView, application use convertView.

 if (convertView == null) {
/ / .......
/ / Your code above
convertView = textView;
else {
if (convertView instanceof TextView) {
((TextView) convertView) setText (group [position]);.
}
}
return convertView;

Reply:
cited a floor u012476249 reply:
need to return the View is convertView Okay ......
API did not say Be sure to return convertView ah, of course, reuse convertView is best, but do not convertView are not right ah.
Reply:
reference to the second floor ncepu307 reply:
here you can not directly return textView, application use convertView
.
 if (convertView == null) {
/ / .......
/ / Your code above
convertView = textView;
else {
if (convertView instanceof TextView) {
((TextView) convertView) setText (group [position]);.
}
}
return convertView;


 @ Override 
public View getGroupView (int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/ / TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate (R.layout.group, null);
TextView textView = (TextView) layout.findViewById (R.id.group);
textView.setText (group [groupPosition]);
if (convertView == null) {
convertView = textView;
} Else
{
if (convertView instanceof TextView) {
((TextView) convertView) setText (group [groupPosition]);.
}
}
return convertView;
still reported the same nice
Reply:
You try to change this:
 @ Override 
public View getGroupView (int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/ / TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate (R.layout.group, null);
TextView textView = (TextView) layout.findViewById (R.id.group);
textView.setText (group [groupPosition]);
if (convertView == null) {
convertView = textView;
} Else
{
if (convertView instanceof TextView) {
((TextView) convertView) setText (group [groupPosition]);.
}
}
return convertView;

Note the error message
Reply:
The first paste the code are not familiar with, is to change this one:
convertView = inflater.inflate (R.layout.group, null);
Reply:
reference to the 6th floor u012476249 reply:
first posted the code, are not familiar with, is to change this one:
convertView = inflater.inflate (R.layout.group, null);

Such a change is right, I do not understand is that returns a View object right, then I return a TextView object Why wrong? Want to assign the parent child object Is there anything wrong with
Reply:
I think the problem is that you did not initialize convertView, this time you try to change the return Textview object to try to see if he has no error slightly, I think it is getView method is automatically called convertView to fill, so the way to return convertView object
Reply:
Your view should be returned to the parent view addView above, and your textview already has a parent view, I think it is reported iew has parent ... wrong

No comments:

Post a Comment