gpt4 book ai didi

android - AdapterView 不支持 java.lang.UnsupportedOperationException : addView(View, LayoutParams)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:20 26 4
gpt4 key购买 nike

我正在使用在网上找到的可扩展 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);
}
}

适配器:

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;
}
}

主.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>

我试过这个解决方案,但没有用:(

Android: Custom ListAdapter extending BaseAdapter crashes on application launch

它被告知要将第三个参数“false”添加到 inflater.inflate(R.layout.group_row, null, false);

最佳答案

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>

AdapterView 的子类(如 ExpandableListView)不能在 xml 布局中有子类(就像您在布局中所做的那样)。

关于android - AdapterView 不支持 java.lang.UnsupportedOperationException : addView(View, LayoutParams),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11154379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com