如何使用两个 List<String> 制作 ExpandableListView

问题描述

我想使用两个 List<String> 制作关于 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);

    }
....

这是我的适配器


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

我怎样才能做到这一点?因为我已经在 String-Array 中写了很多问题和答案。但是在教程中,Title是一一的。

非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...