Android ExpandableList视图如何从“中膨胀数据”;地图<;ArrayList<;字符串>;,对象>&引用;
我正在尝试从“Android ExpandableList视图如何从“中膨胀数据”;地图<;ArrayList<;字符串>;,对象>&引用;,android,hashmap,expandablelistview,Android,Hashmap,Expandablelistview,我正在尝试从“Map”中膨胀数据 在这里,我将标题存储为数组列表,子列表存储为对象。问题是,当我运行代码时,它正在崩溃 这是我的调试控制台: E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! E/AndroidRuntime: Error reporting crash android.os.TransactionTooLargeException at android.os.BinderProxy.transactNative(Native
Map
”中膨胀数据
在这里,我将标题存储为数组列表
,子列表存储为对象
。问题是,当我运行代码时,它正在崩溃
这是我的调试控制台:
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:496)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4164)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chonang.chonangclientlogin, PID: 12175
java.lang.StackOverflowError: stack size 8MB
at com.chonang.chonangclientlogin.listViewAdapter.TestExpandableView.getGroupId(TestExpandableView.java:65)
错误显示在第65行
以下是我的适配器代码:
public TestExpandableView(String allow, Map<ArrayList<String>, Object> headerListArrayList, String country, String state, String city, String tree, Context context) {
this.allow=allow;
this.country=country;
this.state=state;
this.city=city;
this.TREE=tree;
this.data=headerListArrayList;
this.context= context;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return (int) data.get(getChildrenCount(groupPosition));
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return data.get(getChild(groupPosition,childPosition)); // THE ERROR IS INDICATION HERE
}
@Override
public long getGroupId(int groupPosition) {
return (long) data.get(getGroupId(groupPosition));
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) data.get(getGroup(groupPosition));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_view_header, null);
}
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition,isExpanded);
TextView headerName = convertView.findViewById(R.id.headerName);
headerName.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
initializedData(convertView);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_view, null);
}
ItemListProduct data;
data= (ItemListProduct) getChild(groupPosition,childPosition);
itemName.setText(String.valueOf(data.getITEM_NAME()));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private void initializedData(View convertView) {
itemView= convertView.findViewById(R.id.itemView);
itemName = convertView.findViewById(R.id.ListViewItemNameAdapter);
}
}
publictestexpandableview(字符串允许、地图标题列表、字符串国家、字符串状态、字符串城市、字符串树、上下文){
this.allow=允许;
这个国家=国家;
这个状态=状态;
这个。城市=城市;
这棵树=树;
this.data=headerListaryList;
this.context=context;
}
@凌驾
public int getGroupCount(){
返回data.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
return(int)data.get(getChildrenCount(groupPosition));
}
@凌驾
公共对象getGroup(int-groupPosition){
返回组位置;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
return data.get(getChild(groupPosition,childPosition));//此处显示错误
}
@凌驾
公共长getGroupId(int-groupPosition){
return(long)data.get(getGroupId(groupPosition));
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
stringheadertitle=(String)data.get(getGroup(groupPosition));
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)this.context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.expandable_view_header,空);
}
ExpandableListView mExpandableListView=(ExpandableListView)父级;
expandGroup(groupPosition,isExpanded);
TextView headerName=convertView.findViewById(R.id.headerName);
headerName.setText(headerTitle);
返回视图;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
初始化数据(convertView);
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)this.context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.card_视图,空);
}
项目列表产品数据;
数据=(ItemListProduct)getChild(groupPosition,childPosition);
itemName.setText(String.valueOf(data.getITEM_NAME());
返回视图;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回false;
}
私有void初始化数据(视图转换视图){
itemView=convertView.findViewById(R.id.itemView);
itemName=convertView.findViewById(R.id.ListViewItemNameAdapter);
}
}
我制作了一个完整的示例,其作用类似于引导用户从单个数据列表(ArrayList)中找到可用的酒店。试一试,它不会直接回答你的问题,但包含一些有用的东西
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ArrayList<HashMap<String, String>> dataList;
CustomExpandableListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> header = new ArrayList<>();
header.add("Country");
header.add("State");
header.add("City");
header.add("Hotel");
dataList = createSampleDataList(header);
debugSampleDataList(header, dataList);
ExpandableListView expandableListView = findViewById(R.id.eLV);
adapter = new CustomExpandableListViewAdapter(this, header, dataList);
expandableListView.setAdapter(adapter);
expandableListView.expandGroup(0);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
return true;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
for (int j = adapter.getGroupCount() - 1; j > i; j--)
expandableListView.collapseGroup(j);
adapter.updateViewsOnClick(i, i1);
if (i < adapter.getGroupCount() - 1) expandableListView.expandGroup(i + 1);
else {
int index = adapter.getFinalSelectedItemIndex(i1);
Toast.makeText(getApplicationContext(), "(" + index + ")\n" +
dataList.get(index).toString(), Toast.LENGTH_LONG).show();
}
return true;
}
});
}
private ArrayList<HashMap<String, String>> createSampleDataList(ArrayList<String> header) {
ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
for (int i = 0; i < 27; i++) {
HashMap<String, String> data = new HashMap<>();
data.put(header.get(0), header.get(0) + (i % 5 + 1));
data.put(header.get(1), (i % 5 + 1) + header.get(1) + (i % 4 + 1));
data.put(header.get(2), (i % 5 + 1) + header.get(2) + (i % 3 + 1));
data.put(header.get(3), (i % 5 + 1) + header.get(3) + (i % 2 + 1));
// If more items in header, add data fields here accordingly.
dataList.add(data);
}
return dataList;
}
// Printing the whole data list to Logcat for debug.
private void debugSampleDataList(ArrayList<String> header, ArrayList<HashMap<String, String>> dataList) {
String msg = "\n";
for (HashMap<String, String> data : dataList) {
for (String heading : header) {
msg += data.get(heading) + " ";
}
msg += "\n";
}
Log.d("debug", msg);
}
}
public类MainActivity扩展了AppCompatActivity{
ArrayList数据表;
自定义可扩展列表视图适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList标头=新的ArrayList();
标题。添加(“国家”);
标题。添加(“州”);
标题。添加(“城市”);
标题。添加(“酒店”);
dataList=createSampleDataList(标题);
debugSampleDataList(标题、数据列表);
ExpandableListView ExpandableListView=findViewById(R.id.eLV);
adapter=新的CustomExpandableListViewAdapter(此、标题、数据列表);
expandableListView.setAdapter(适配器);
expandableListView.expandGroup(0);
expandableListView.setOnGroupClickListener(新的expandableListView.OnGroupClickListener(){
@凌驾
公共布尔onGroupClick(ExpandableListView ExpandableListView,View视图,int i,long l){
返回true;
}
});
expandableListView.setOnChildClickListener(新的expandableListView.OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView ExpandableListView,View视图,int i,int i1,long l){
对于(int j=adapter.getGroupCount()-1;j>i;j--)
可扩展列表视图。折叠组合(j);
adapter.updateViewsOnClick(i,i1);
如果(i
自定义可扩展ListVie
public class CustomExpandableListViewAdapter extends BaseExpandableListAdapter {
Activity context;
ArrayList<String> groupList;
ArrayList<HashMap<String, String>> childList;
ArrayList<ArrayList<ArrayList<HashMap<String, String>>>> childListInViewArray = new ArrayList<>();
int[] selectedPositions;
LayoutInflater inflater;
public CustomExpandableListViewAdapter(Activity context, ArrayList<String> groupList, ArrayList<HashMap<String, String>> childList) {
this.context = context;
this.groupList = groupList;
this.childList = childList;
inflater = this.context.getLayoutInflater();
selectedPositions = new int[groupList.size()];
for (int i = 0; i < selectedPositions.length; i++) selectedPositions[i] = -1;
ArrayList<HashMap<String, String>> singleChild;
ArrayList<ArrayList<HashMap<String, String>>> childListInView = new ArrayList<>();
for (HashMap<String, String> rawData : childList) {
boolean found = false;
int foundPosition = -1;
if (childListInView.size() > 0) {
for (foundPosition = 0; foundPosition < childListInView.size(); foundPosition++) {
if (childListInView.get(foundPosition).get(0).get(groupList.get(0)).equals(rawData.get(groupList.get(0)))) {
singleChild = childListInView.get(foundPosition);
singleChild.add(rawData);
found = true;
break;
}
}
if (!found) {
singleChild = new ArrayList<>();
singleChild.add(rawData);
childListInView.add(singleChild);
}
} else {
singleChild = new ArrayList<>();
singleChild.add(rawData);
childListInView.add(singleChild);
}
}
childListInViewArray.add(childListInView);
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int i) {
return childListInViewArray.get(i).size();
}
@Override
public String getGroup(int i) {
return groupList.get(i);
}
@Override
public ArrayList<HashMap<String, String>> getChild(int i, int i1) {
return childListInViewArray.get(i).get(i1);
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.item_group, null);
}
TextView tv = view.findViewById(R.id.group);
tv.setText(getGroup(i));
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.item_child, null);
}
TextView tv = view.findViewById(R.id.child);
ArrayList<HashMap<String, String>> child = getChild(i, i1);
tv.setText(child.get(0).get(groupList.get(i)) + " (" + childListInViewArray.get(i).get(i1).size() + ")");
if (selectedPositions[i] == i1) tv.setTextColor(Color.RED);
else tv.setTextColor(Color.BLACK);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
public void updateViewsOnClick(int group, int child) {
for (int i = group; i < selectedPositions.length; i++) selectedPositions[i] = -1;
selectedPositions[group] = child;
for (int i = childListInViewArray.size() - 1; i > group; i--)
childListInViewArray.remove(i);
if (group == groupList.size() - 1) {
notifyDataSetChanged();
} else {
updateNextGroup(group, child);
}
}
private void updateNextGroup(int group, int child) {
ArrayList<HashMap<String, String>> singleChild;
ArrayList<ArrayList<HashMap<String, String>>> childListInView = new ArrayList<>();
for (HashMap<String, String> rawData : childListInViewArray.get(group).get(child)) {
boolean found = false;
int foundPosition = -1;
if (childListInView.size() > 0) {
for (foundPosition = 0; foundPosition < childListInView.size(); foundPosition++) {
if (childListInView.get(foundPosition).get(0).get(groupList.get(group + 1)).equals(rawData.get(groupList.get(group + 1)))) {
singleChild = childListInView.get(foundPosition);
singleChild.add(rawData);
found = true;
break;
}
}
if (!found) {
singleChild = new ArrayList<>();
singleChild.add(rawData);
childListInView.add(singleChild);
}
} else {
singleChild = new ArrayList<>();
singleChild.add(rawData);
childListInView.add(singleChild);
}
}
childListInViewArray.add(group + 1, childListInView);
notifyDataSetChanged();
}
public int getFinalSelectedItemIndex(int child) {
HashMap<String, String> selectedData = childListInViewArray.get(groupList.size() - 1).get(child).get(0);
return childList.indexOf(selectedData);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/eLV"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:textSize="30sp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:textSize="20sp"/>
</LinearLayout>