I am using Expandable ListView example found on net
我正在使用可扩展的ListView示例
Activity:
活动:
public class ExpandableListViewActivity extends ExpandableListActivity {
/**
* strings for group elements
*/
static final String arrGroupelements[] = { "India", "Australia", "England",
"South Africa" };
/**
* strings for child elements
*/
static final String arrChildelements[][] = {
{ "Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
{ "Ponting", "Adam Gilchrist", "Michael Clarke" },
{ "Andrew Strauss", "kevin Peterson", "Nasser Hussain" },
{ "Graeme Smith", "AB de villiers", "Jacques Kallis" } };
DisplayMetrics metrics;
int width;
ExpandableListView expList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expList = getExpandableListView();
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
// this code for adjusting the group indicator into right side of the
// view
expList.setIndicatorBounds(width - GetDipsFromPixel(50), width
- GetDipsFromPixel(10));
expList.setAdapter(new ExpAdapter(this));
expList.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
Log.e("onGroupExpand", "OK");
}
});
expList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
public void onGroupCollapse(int groupPosition) {
Log.e("onGroupCollapse", "OK");
}
});
expList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.e("OnChildClickListener", "OK");
return false;
}
});
}
public int GetDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
Adapter:
适配器:
public class ExpAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpAdapter(Context context) {
myContext = context;
}
public Object getChild(int groupPosition, int childPosition) {
return null;
}
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView tvPlayerName = (TextView) convertView
.findViewById(R.id.tvPlayerName);
tvPlayerName
.setText(ExpandableListViewActivity.arrChildelements[groupPosition][childPosition]);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return ExpandableListViewActivity.arrChildelements[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return null;
}
public int getGroupCount() {
return ExpandableListViewActivity.arrGroupelements.length;
}
public long getGroupId(int groupPosition) {
return 0;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_row, null);
}
TextView tvGroupName = (TextView) convertView
.findViewById(R.id.tvGroupName);
tvGroupName
.setText(ExpandableListViewActivity.arrGroupelements[groupPosition]);
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
main.xml:
main。xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@drawable/group_indicator" >
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Items" >
</TextView>
</ExpandableListView>
</LinearLayout>
I tried this solution, but didn't work :(
我尝试了这个办法,但没有成功。
Android: Custom ListAdapter extending BaseAdapter crashes on application launch
Android:自定义ListAdapter扩展BaseAdapter在应用程序启动时崩溃
there its told to add a third parameter "false", to the inflater.inflate(R.layout.group_row, null, false);
在那里,它被告知在通货膨胀中增加第三个参数“假”。group_row,null,假);
2 个解决方案
#1
17
Move the TextView
outside the ExpandableListView
element:
将TextView移出ExpandableListView元素:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@drawable/group_indicator" >
</ExpandableListView>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Items" >
</TextView>
</LinearLayout>
Subclasses of AdapterView
(like ExpandableListView
) can't have children in a xml layout(like you did in your layout).
AdapterView的子类(比如ExpandableListView)不能在xml布局中包含子类(就像您在布局中所做的那样)。
#2
0
If you're like me and found this question without using ExpandableListView
, try using adding the third parameter to the inflate
method, as @Luksprog mentions, also mentioned here:
如果您和我一样,并且在没有使用ExpandableListView的情况下发现了这个问题,请尝试使用添加第三个参数到膨胀方法,如@Luksprog提到的,这里也提到了:
inflater.inflate(R.layout.group_row, parent, false);
Don't use true
instead of false
or you will get the same error that brought you here, because it's trying to attach/add it to the parent, hence the addView is not supported in AdapterView
error.
不要使用true而不是false,否则您将得到与本文相同的错误,因为它试图将其附加/添加到父节点,因此AdapterView错误不支持addView。
#1
17
Move the TextView
outside the ExpandableListView
element:
将TextView移出ExpandableListView元素:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="@drawable/group_indicator" >
</ExpandableListView>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Items" >
</TextView>
</LinearLayout>
Subclasses of AdapterView
(like ExpandableListView
) can't have children in a xml layout(like you did in your layout).
AdapterView的子类(比如ExpandableListView)不能在xml布局中包含子类(就像您在布局中所做的那样)。
#2
0
If you're like me and found this question without using ExpandableListView
, try using adding the third parameter to the inflate
method, as @Luksprog mentions, also mentioned here:
如果您和我一样,并且在没有使用ExpandableListView的情况下发现了这个问题,请尝试使用添加第三个参数到膨胀方法,如@Luksprog提到的,这里也提到了:
inflater.inflate(R.layout.group_row, parent, false);
Don't use true
instead of false
or you will get the same error that brought you here, because it's trying to attach/add it to the parent, hence the addView is not supported in AdapterView
error.
不要使用true而不是false,否则您将得到与本文相同的错误,因为它试图将其附加/添加到父节点,因此AdapterView错误不支持addView。