如何在ListAdapter中获取特定子按钮的引用

时间:2017-08-03 20:17:14

标签: android android-adapter

我有一个扩展release-3.0的类,我在每组上都创建了一个按钮。共有6组。在BaseExpandableListAdapter上的适配器中,我想更改特定位置上的按钮文本(例如2)。

enter image description here

我听说过标签功能,但我不知道如何在我的情况下使用它。如何更改getChildView上的button text

specific child

}

1 个答案:

答案 0 :(得分:0)

package com.example.adapter;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;



import com.example.R;
import com.example.model.filter_Item;


import org.json.JSONObject;


import java.util.ArrayList;
import java.util.HashMap;


public class Filter_ExpandableAdapter extends BaseExpandableListAdapter {
    private Context _context;
    ArrayList<String> _listDataHeader;




    HashMap<Integer, ArrayList<filter_Item>> list_child = new HashMap<>();
    private HashMap<Integer, boolean[]> mChildCheckStates;
    public static ArrayList<filter_Item> selected_list_child = new ArrayList<>();
    private ChildViewHolder childViewHolder;



    public Filter_ExpandableAdapter(Context _context, ArrayList<String> _listDataHeader, HashMap<Integer, ArrayList<filter_Item>> list_child) {
        this._context = _context;
        this._listDataHeader = _listDataHeader;
        this.list_child = list_child;
        mChildCheckStates = new HashMap<Integer, boolean[]>();

    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return list_child.get(groupPosition).size();

    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list_child.get(groupPosition).get(childPosition);


    }


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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.header_row_filter, null);
        }
        TextView lblListHeader = (TextView) convertView.findViewById(R.id.filter_name);
        lblListHeader.setText(_listDataHeader.get(groupPosition).toString());

        return convertView;
    }


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

        final int mGroupPosition = groupPosition;
        final int mChildPosition = childPosition;


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_row_filter, null);
            childViewHolder = new ChildViewHolder();
            childViewHolder.select_item = (CheckBox) convertView.findViewById(R.id.select_checkbox);
            convertView.setTag(R.layout.child_row_filter, childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag(R.layout.child_row_filter);
        }

        childViewHolder.select_item.setText("" + list_child.get(groupPosition).get(childPosition).getLc_sub_name());
        childViewHolder.select_item.setOnCheckedChangeListener(null);


        if (mChildCheckStates.containsKey(mGroupPosition)) {
            boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
            childViewHolder.select_item.setChecked(getChecked[mChildPosition]);

        }
        else {
            boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
            mChildCheckStates.put(mGroupPosition, getChecked);
            childViewHolder.select_item.setChecked(false);
        }


        childViewHolder.select_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                    getChecked[mChildPosition] = isChecked;
                    mChildCheckStates.put(mGroupPosition, getChecked);
                    selected_list_child.add(list_child.get(groupPosition).get(childPosition));
                }
                else
                {
                    boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                    getChecked[mChildPosition] = isChecked;
                    mChildCheckStates.put(mGroupPosition, getChecked);
                    selected_list_child.remove(list_child.get(groupPosition).get(childPosition));
                }
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    private class ChildViewHolder {
        CheckBox select_item;

    }




}

Try this code...