具有可扩展Listview的自定义适配器

时间:2014-02-03 19:49:45

标签: java android android-listview adapter expandablelistview

任何人都可以帮助我...我正在尝试动态实现可扩展的listview,并使用其中一个教程(Androidhive)来熟悉它。我的问题是我从parse.com获取数据,我想将其动态加载到可扩展的列表视图中。首先,我从解析到每个类型分类的对象的数据,然后我想将其转换为可扩展的列表视图。我被困在的是我不知道如何向孩子传递多于1个对象的适配器(我对每个孩子有3个文本视图)。从下面的例子中我得到了主要道路的数据,仅从孩子身上看,但我仍然需要获得2个额外的值,我不知道如何实现。

下面是我的asynctask代码:

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        // mProgressDialog.setTitle("Loading...");
        // Set progressdialog message
        mProgressDialog.setMessage("wait please ...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array

        listviewPostsGet = new ArrayList<listviewPostsGet>();


        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "MainRoadsList");

        try {

            ob = query.find();

            for (ParseObject country : ob) {

                listviewPostsGet map = new listviewPostsGet();
                map.setmainroad(((String) country.get("mainroad")));
                map.setmainroadfrom(((String) country.get("from")));
                map.setmainroadto((String) country.get("to"));
                map.setcategory(((String) country.get("mainroad")));

                listviewPostsGet.add(map);

                listDataHeader = new ArrayList<String>();
                listDataChild = new HashMap<String, List<String>>();


                String check = "";

                for (int i = 0; i < listviewPostsGet.size(); i++)

                {
                    String XX = listviewPostsGet.get(i).getcategory()
                            .toString();

                    if (!check.contentEquals(XX.toString())) {

                        listDataHeader.add(XX);

                        check = XX;

                         List<String> TTT = new ArrayList<String>();

                        for (int j = 0; j < listviewPostsGet.size(); j++) {


                            String YY = listviewPostsGet.get(j).getcategory().toString();

                            if (YY.contentEquals(XX.toString()))

                            {

                                TTT.add(listviewPostsGet.get(j).getmainroadfrom().toString()+" "+listviewPostsGet.get(j).getmainroadto().toString());

                                listDataChild.put(XX, TTT);

                            }
                        }

                    }

                }

            }

        }

        catch (ParseException e)

        {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        listAdapter = new ExpandableListAdapter1(MainActivity.this,
                listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        mProgressDialog.dismiss();

    }
}

此处还有适配器:

public class ExpandableListAdapter1 extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter1(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listDataChild ) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listDataChild;
}


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


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


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.mainlayout, null);
    }

    TextView from = (TextView) convertView
            .findViewById(R.id.fromroad);

    TextView to = (TextView) convertView
            .findViewById(R.id.toroad);

    from.setText(childText);


    return convertView;
}


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


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


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


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


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.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}


public boolean hasStableIds() {
    return false;
}

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

}

编辑:

要清楚,我的问题是我想将listviewPostsGet中的3个值分配给可扩展的listview子项,而我实际可以实现的是只传递1个值,即“主路从”到TTT数组列表然后将其分配给适配器上的相应textview。

0 个答案:

没有答案
相关问题