gpt4 book ai didi

java - 我尝试将 ExpandableListView 与自定义适配器一起使用,但无法显示任何内容

转载 作者:行者123 更新时间:2023-12-01 21:27:44 25 4
gpt4 key购买 nike

我正在尝试使用 ExpandableListView using this tutorial显示物理常数及其信息。我已经为子项、父项以及我希望在其中显示列表的 Activity 制作了布局。我的数据存储在 MyDataProvider 类的 HashMap 中。我相信这一切都很好并且应该有效。

我认为导致问题的部分是适配器,它是我自己编写的并包含在下面。问题是,当我进行此 Activity 时,应用程序不会崩溃,而只是显示完全空白的白屏。我还看了this tutorial如果这能让任何人更好地了解。

这是我的适配器:

package com.gmd.referenceapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by gmd on 6/13/2016.
*/
public class MyCustomAdapter extends BaseExpandableListAdapter {

private Context context;
private HashMap<String,ListViewItem> constantsHashMap;
private List<String> constantList;

public MyCustomAdapter(Context context, HashMap<String, ListViewItem> hashMap, List<String> list){

constantsHashMap = hashMap;
this.context = context;
this.constantList = list;

}



public View getChildView(int groupPosition,
int childPosition,
boolean isLastChild,View convertView, ViewGroup parent) {
String childTitle = (String) getChild(groupPosition, childPosition).toString();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout, parent, false);
}
TextView childTextView = (TextView) convertView.findViewById(R.id.textViewChild);
childTextView.setText(childTitle);
return convertView;


}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}

@Override
public int getGroupCount() {
return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
return 0;
}

@Override
public String getGroup(int groupPosition) {
return constantList.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return constantsHashMap.get(this.constantList.get(groupPosition));
}


@Override
public long getGroupId(int groupPosition) {
return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String groupTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.parent_layout, parent, false);
}
TextView parentTextView = (TextView) convertView.findViewById(R.id.textViewParent);
parentTextView.setText(groupTitle);
return convertView;
}
}

如果我包含我试图在此处显示列表的类,它似乎也可能很有用:

package com.gmd.referenceapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.gmd.referenceapplication.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CommonConstants extends AppCompatActivity {
View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_constants);


MyDataProvider dp = new MyDataProvider();

ExpandableListView view;
view = (ExpandableListView) findViewById(R.id.expandableList);
HashMap constantsHashMap;
constantsHashMap = dp.getCommonMap();
ArrayList constantsHashMapKeys = new ArrayList<String>(constantsHashMap.keySet());

MyCustomAdapter adapter = new MyCustomAdapter(this, constantsHashMap, constantsHashMapKeys);
view.setAdapter(adapter);










}
}

我对此做了相当多的研究,但我仍然不确定我做错了什么,所以我将不胜感激任何和所有的帮助。谢谢!

最佳答案

您返回的组和子计数为 0。该计数用于渲染组及其子项。试试这个

@Override
public int getGroupCount() {
return this.constantList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return 1; // Looks like you have only one child in each group, change this if not
}

关于java - 我尝试将 ExpandableListView 与自定义适配器一起使用,但无法显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37821235/

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