Android 如何使用两个列表创建ExpandableListView<;字符串>;
我想使用两个Android 如何使用两个列表创建ExpandableListView<;字符串>;,android,expandablelistview,Android,Expandablelistview,我想使用两个List对ExpandableListView进行常见问题解答,但它显示了错误:尝试在空对象引用上调用接口方法“int java.util.List.size()” 以下是我的活动: List<String> itemGroup; List<String> itemChild; HashMap<String, List<String>> faqList = new HashMap<>(); ExpandableListVi
List
对ExpandableListView进行常见问题解答,但它显示了错误:尝试在空对象引用上调用接口方法“int java.util.List.size()”
以下是我的活动:
List<String> itemGroup;
List<String> itemChild;
HashMap<String, List<String>> faqList = new HashMap<>();
ExpandableListView expandableListView;
ExpandableListAdapter exAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.e_faq_activity);
expandableListView = findViewById(R.id.expanableFAQ);
itemGroup = Arrays.asList(getResources().getStringArray(R.array.itemQuestion));
itemChild = Arrays.asList(getResources().getStringArray(R.array.itemAnswer));
faqList.put(String.valueOf(itemGroup), itemChild);
exAdapter = new CustomExpandableListAdapter(this, itemGroup, faqList);
expandableListView.setAdapter(exAdapter);
}
....
列表项目组;
列出项目子项;
HashMap faqList=新建HashMap();
ExpandableListView ExpandableListView;
可扩展列表适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.e_faq_活动);
expandableListView=findViewById(R.id.expanableFAQ);
itemGroup=Arrays.asList(getResources().getStringArray(R.array.itemQuestion));
itemChild=Arrays.asList(getResources().getStringArray(R.array.itemAnswer));
faqList.put(String.valueOf(itemGroup),itemChild);
exAdapter=新的CustomExpandableListAdapter(此、itemGroup、faqList);
expandableListView.setAdapter(exAdapter);
}
....
这是我的适配器
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private final Context context;
private final List<String> expandableListQuestion;
private final HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListQuestion,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListQuestion = expandableListQuestion;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(expandableListQuestion.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String mListAnswer = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_faq_child, null);
}
TextView expandedListAnswer = (TextView) convertView
.findViewById(R.id.faqAnswer);
expandedListAnswer.setText(mListAnswer);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListQuestion.get(listPosition))
.size(); // THE ERROR IS HERE
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListQuestion.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListQuestion.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String mListQuestion = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.item_faq_group, null);
}
TextView listTitleQuestion = (TextView) convertView
.findViewById(R.id.faqQuestion);
listTitleQuestion.setTypeface(null, Typeface.BOLD);
listTitleQuestion.setText(mListQuestion);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
公共类CustomExpandableListAdapter扩展了BaseExpandableListAdapter{
私人最终语境;
私人最终列表可扩展列表问题;
私有最终HashMap expandableListDetail;
公共CustomExpandableListAdapter(上下文,列表expandableListQuestion,
HashMap expandableListDetail){
this.context=上下文;
this.expandableListQuestion=expandableListQuestion;
this.expandableListDetail=expandableListDetail;
}
@凌驾
公共对象getChild(int listPosition,int expandedListPosition){
返回此.expandableListDetail.get(expandableListQuestion.get(listPosition))
.get(expandedListPosition);
}
@凌驾
公共长getChildId(int-listPosition、int-expandedListPosition){
返回expandedListPosition;
}
@凌驾
公共视图getChildView(int listPosition,最终int expandedListPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串mListAnswer=(字符串)getChild(listPosition,expandedListPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context
.getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.item\u faq\u子项,空);
}
TextView expandedListAnswer=(TextView)convertView
.findviewbyd(R.id.faq答案);
expandedListAnswer.setText(mListAnswer);
返回视图;
}
@凌驾
公共int getChildrenCount(int listPosition){
返回this.expandableListDetail.get(this.expandableListQuestion.get(listPosition))
.size();//错误在这里
}
@凌驾
公共对象getGroup(int listPosition){
返回此.expandableListQuestion.get(listPosition);
}
@凌驾
public int getGroupCount(){
返回此.expandableListQuestion.size();
}
@凌驾
公共长getGroupId(int-listPosition){
返回列表位置;
}
@凌驾
公共视图getGroupView(int listPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串mListQuestion=(字符串)getGroup(listPosition);
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context。
getSystemService(上下文布局\充气机\服务);
convertView=LayoutFlater.充气(R.layout.item\u faq\u组,空);
}
TextView列表标题问题=(TextView)convertView
.findviewbyd(R.id.faq问题);
listTitleQuestion.setTypeface(null,Typeface.BOLD);
listitlequestion.setText(mListQuestion);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int listPosition,int expandedListPosition){
返回true;
}
}
我怎样才能做到这一点?因为我已经在字符串数组中写了很多问题和答案。但在教程中,标题是一个接一个的
非常感谢。我正在使用来自的图书馆。它很容易实现,具有相对更大的灵活性。