从mysql获取数据并在android中的ExpandableListView中显示数据

时间:2017-03-06 10:16:24

标签: android expandablelistview expandablelistadapter

我访问了相同主题的链接,但没有一个人回答我的问题。我不想仅使用简单的可扩展列表视图来创建自定义可扩展列表视图。我已经成功地从mysql数据库中获取数据并验证了使用toast消息检索的所有值,并使用postman测试了php脚本。
现在我想在ExpandableListView中显示获取的值 这是list_child.xml文件

<?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/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"/>

</LinearLayout>

我正在使用一个函数从数据库中获取值,我得到的值,但我无法在ExpandableListView中打印这些值。此功能准备可扩展列表视图

private void ListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String,List<String>>();

    //Adding header to expandable list view
    listDataHeader.add("ABC");
    listDataHeader.add("DEF");

    //Adding child data
    List<String> abc = new ArrayList<>();
    abc.add(fetchAbc());
    List<String> def = new ArrayList<>();
    def.add(fetchDef());


    //Header child data
    listDataChild.put(listDataHeader.get(0),abc);
    listDataChild.put(listDataHeader.get(1),def);
}
    private String fetchAbc() {

    return ("first name : \t"+Name+"\nEmail : \t"+email);
}

private String fetchDef() {

    return ("Id : \t"+id);
}

在可扩展列表视图中,我从上面的函数中获取子视图,如名字,电子邮件和Id。但不是价值观。这是ExpandableListAdapter类文件

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> _listDataHeader;
    private HashMap<String,List<String>> _listDataChild;
    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.header);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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

谢谢。

1 个答案:

答案 0 :(得分:0)

private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();
    url = ip.ip+"loadcourse.php";
    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {



                    try {
                        JSONArray data = new JSONArray(response);
                        for (int i = 0; i < data.length(); i++) {


                            JSONObject c = data.getJSONObject(i);
                            one = c.getString("dname").split(",");
                            two = c.getString("course_name");
                            listDataHeader.add(String.valueOf(one[0]));


                            JSONArray dt= new JSONArray(two.toString());
                            List<String> nowShowing  = new ArrayList<String>();
                            for (int j = 0; j < dt.length(); j++) {


                                JSONObject d = dt.getJSONObject(j);

                                three = d.getString("course").split(",");

                                nowShowing.add(String.valueOf(three[0]));


                            }
                            listDataChild.put(listDataHeader.get(i), nowShowing); 










                        }




                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    // response
                    Log.d("Response", response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", error.toString());
                }
            }
    ) {
       @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

           params.put("cid",ip.id);
           return params;
        }
    };
    queue.add(postRequest);