如何在expandablelistview中为子项添加按钮?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在expandablelistview中为子项添加按钮?相关的知识,希望对你有一定的参考价值。

对不起,这是java编程的新手。所以这里......我有一个可扩展的列表视图,它是通过JSON数组从我的mysql数据库中填充的。数据库是他们要求的人员和礼物的列表。一切顺利。我现在想给孩子添加一个按钮,但前提是尚未购买礼物。

这是我的代码(删节)

expandable list adapter.Java

 public class ExpandableListAdapter 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 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;
    }

    private int getItemId() {
        int position = 0;
        return position;
    }

    @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.lblListHeader);
        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;
    }
}

main activity.Java

 public class MainActivity extends ProfileActivity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    private ProgressDialog mprocessingdialog;
    private static String url = "http://www.wishlist.feichter.co.uk/android/present_list.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mprocessingdialog = new ProgressDialog(this);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        new DownloadJason().execute();
    }

    /*
     * Preparing the list data
     */
    private class DownloadJason extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            //Showing Progress dialog
            mprocessingdialog.setTitle("Please Wait..");
            mprocessingdialog.setMessage("Loading");
            mprocessingdialog.setCancelable(false);
            mprocessingdialog.setIndeterminate(false);
            mprocessingdialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            JSONParser jp = new JSONParser();
            String jsonstr = jp.makeServiceCall(url);
            Log.d("Response = ", jsonstr);

            if (jsonstr != null) {
                // For Header title Arraylist
                listDataHeader = new ArrayList<String>();

                // Hashmap for child data key = header title and value = Arraylist (child data)
                listDataChild = new HashMap<String, List<String>>();

                try {
                    JSONObject jobj = new JSONObject(jsonstr);
                    JSONArray jarray = jobj.getJSONArray("presents_db");

                    for (int hk = 0; hk < jarray.length(); hk++) {
                        JSONObject d = jarray.getJSONObject(hk);

                        // Adding Header data
                        //listDataHeader.add(d.getString("name"));
                        listDataHeader.add(d.getString("name") + "'s presents are: ");

                        // Adding child data for lease offer
                        List<String> members = new ArrayList<String>();

                        //members.add(d.getString("name") + "'s presents are: ");

                        JSONArray xarray = d.getJSONArray("presents_list");
                        for (int i = 0; i < xarray.length(); i++) {
                            JSONObject a = xarray.getJSONObject(i);
                            if("".equals(a.getString("bought_by"))) {
                                members.add(a.getString("present"));
                                //members.add(System.getProperty("line.separator"));
                                //ExpandableListAdapter hello = new test(this);
                                //hello.execute();
                                //View b = findViewById(R.id.BtnToClick);
                                //Button b = findViewById(R.id.BtnToClick);
                                //b.setVisibility(View.VISIBLE);
                            }else{
                                members.add(a.getString("present") + "
 bought by: " + a.getString("bought_by"));
                            }
                        }

                        // Header into Child data
                        listDataChild.put(listDataHeader.get(hk), members);
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getApplicationContext(),"Please Check internet Connection", Toast.LENGTH_SHORT).show();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            mprocessingdialog.dismiss();

            //call constructor
            listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild);

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

正如您所看到的,if("".equals(a.getString("bought_by"))) {函数会更改孩子显示的内容,但是,如何添加按钮?

我知道它必须是一种完全不同的方法......

任何帮助非常感谢!

答案

在child_layout中添加按钮,使按钮的可见性“消失”。在getChildView()中,检查是否已购买礼物并使按钮可见,在其他情况下,使按钮可见性“消失”。

以上是关于如何在expandablelistview中为子项添加按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 ExpandableListView 的子项中删除分隔符?

expandableListView 子项之间的干扰

Android - 在 ExpandableListView 中查看每个组的特定子项

如何在sbt shell中为所有子项目设置一个值?

如何在具有多个子项目的 Visual Studio 中为 C# 解决方案设置通用值(抽象)?

当父母在Android中折叠时如何更改可扩展列表视图子项的视图?