可扩展列表ClassCastException

时间:2016-10-25 06:47:08

标签: android json expandablelistview expandablelistadapter

朋友们,我正在尝试使用下面的JSON进行可扩展列表视图

http://sampletemplates.net.in/doctorapp/index.php?component=json&action=getfaq

但我在BaseExpandableListAdapter(适配器类)中收到此错误我无法理解其中的错误请帮助我

java.lang.ClassCastException:java.util.ArrayList无法强制转换为doctor.Doc_Faq_subitems 在doctor.Doc_Faq_Adapter.getChildView(Doc_Faq_Adapter.java:94)

Faq.class

public class Faq extends BaseActivity(AppCompatActivity){
    TextView question,answers;
    ExpandableListView faqlist;
    Doc_Faq_Adapter listadapter;
    ArrayList<Doc_Faq_Data> list_one;
    ArrayList<Doc_Faq_subitems> childlist;
    @BindView(R.id.popularcard)
    CardView popularcard;
    @BindView(R.id.answer)
    TextView answer;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doc_faq);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle("FAQ");
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        question = (TextView) findViewById(R.id.question);
        answers = (TextView) findViewById(R.id.answer);
        faqlist = (ExpandableListView) findViewById(R.id.faqlist);
        list_one = new ArrayList<Doc_Faq_Data>();
        childlist=new ArrayList<>();
        listadapter = new Doc_Faq_Adapter(getApplicationContext(),list_one);
        faqlist.setAdapter(listadapter);
        faq();

    }
    private void faq() {
        String faqdata = Constant.url + "getfaq";
        Log.d("faq", faqdata);
        JsonObjectRequest first = new JsonObjectRequest(Request.Method.GET, faqdata, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray question = response.getJSONArray("data");
                    for (int i=0;i<question.length();i++){
                        JSONObject answer = question.getJSONObject(i);
                        Doc_Faq_Data qnqans = new Doc_Faq_Data();
                        qnqans.setQuestion(answer.getString("question"));
                        Doc_Faq_subitems subitems=new Doc_Faq_subitems();
                        subitems.setAnswer(answer.getString("answer"));
                        childlist.add(subitems);
                        qnqans.setItems(childlist);
                        list_one.add(qnqans);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                listadapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        first.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(first);
    }
}

FaqAdapter.class

public class FaqAdapter extends BaseExpandableListAdapter {

    Context faqcontext;
    private ArrayList<Doc_Faq_Data> faq_datas;

    public Doc_Faq_Adapter(Context faqcontext, ArrayList<Doc_Faq_Data> faq_datas) {
        this.faqcontext = faqcontext;
        this.faq_datas = faq_datas;
    }
    static class Groupname {
        private TextView group_name;

    }

    static class Childnames {
        private TextView item_name;
    }

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

    @Override
    public int getChildrenCount(int i) {
        ArrayList<Doc_Faq_subitems> doc_faq_datas = faq_datas.get(i).getItems();
        return doc_faq_datas.size();
    }

    @Override
    public Object getGroup(int i) {
        return faq_datas.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        ArrayList<Doc_Faq_subitems> doc_faq_subitemses = faq_datas.get(i).getItems();
        return doc_faq_subitemses;
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {
        Groupname groupname;
        Doc_Faq_Data questring=faq_datas.get(i);
        if (convertView != null && convertView.getTag() instanceof Groupname) {
            groupname = (Groupname) convertView.getTag();
        }else {
            LayoutInflater groupinflater= (LayoutInflater) this.faqcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=groupinflater.inflate(R.layout.doc_faqlist_row,viewGroup,false);
            groupname=new Groupname();
            groupname.group_name=(TextView) convertView.findViewById(R.id.question);
            convertView.setTag(groupname);
        }
        groupname.group_name.setText(questring.getQuestion());
        return convertView;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
        Childnames childnames;
        Doc_Faq_subitems childstring= (Doc_Faq_subitems) getChild(i,i1);///Here I am getting the exception 
        if (convertView != null && convertView.getTag() instanceof Childnames) {
            childnames = (Childnames) convertView.getTag();
        } else {
            LayoutInflater groupinflater= (LayoutInflater) this.faqcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=groupinflater.inflate(R.layout.doc_faqlist_row_sub
                    ,viewGroup,false);
            childnames = new Childnames();
            childnames.item_name = (TextView) convertView.findViewById(R.id.answer);
            convertView.setTag(childnames);
        }
        childnames.item_name.setText(childstring.getAnswer());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}

这是标题和子项的两个getter和setter方法

public class Faq_Head{
    String question;
    private ArrayList<Doc_Faq_subitems> items;


    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public ArrayList<Doc_Faq_subitems> getItems() {
        return items;
    }

    public void setItems(ArrayList<Doc_Faq_subitems> items) {
        this.items = items;
    }
}


public class Faq_subitems {
    String answer;

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

0 个答案:

没有答案