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

问题描述 投票:0回答:1

对不起,这是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") + "\r\n 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"))) {函数会更改孩子显示的内容,但是,如何添加按钮?

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

任何帮助非常感谢!

android button expandablelistadapter
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.