Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击expandablelistview中的按钮_Java_Android_Xml - Fatal编程技术网

Java 单击expandablelistview中的按钮

Java 单击expandablelistview中的按钮,java,android,xml,Java,Android,Xml,ExpandableListAdapter 您好,单击按钮时,我需要将onclick事件发送到List_item.xml中的按钮。 我从java和android开发开始。我正在搜索解决方案3天 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List < String > _listDataHeader; // heade

ExpandableListAdapter 您好,单击按钮时,我需要将onclick事件发送到List_item.xml中的按钮。 我从java和android开发开始。我正在搜索解决方案3天

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);
    Button btnListChild = (Button) convertView.findViewById(R.id.btn);


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


    return convertView;
}

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

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

        
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境(private Context)(私人语境);;
私有列表\u listDataHeader;//标题
//标题标题、子标题格式的子数据
私有HashMap\u listDataChild;
公共ExpandableListAdapter(上下文,列表listDataHeader,
HashMap>listChildData){
这._context=context;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView.findViewById(R.id.lblListItem);
按钮btnListChild=(按钮)convertView.findViewById(R.id.btn);
setText(childText);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
TextView lblistheader=(TextView)convertView
.findviewbyd(R.id.lblistheader);
lblListHeader.setTypeface(空);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
list_item.xml-有一个按钮

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="vertical"
    android:background="@color/colorACD_white"
    >

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="144dp"
        android:layout_height="42dp"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="0dp"
        android:gravity="center"
        android:paddingLeft="25dp"
        android:background="@color/colorACD_white"
        android:textColor="@color/colorACD_black"
        android:textSize="18sp"
        android:fontFamily="@font/sourcesansproregular"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="115dp"
        android:layout_height="45dp"
        android:layout_marginLeft="250dp"
        android:fontFamily="@font/robotoregular"
        android:text="Edit">
    </Button>

    <LinearLayout
        android:id="@+id/line"
        android:layout_width="391dp"
        android:layout_height="10dp"
        android:orientation="horizontal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="popup"
        android:orientation="horizontal" />

</RelativeLayout>

Dialog.xml-单击列表项中的按钮时要打开的窗口

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="156dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="133dp"
        android:layout_marginTop="297dp"
        android:layout_marginEnd="121dp"
        android:layout_marginBottom="312dp"
        android:background="@color/colorACD_orange" />

</RelativeLayout>

**My Dialog Java class**

    package com.example.application.example;
public class ExampleDialog extends AppCompatDialogFragment {
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Information")
                .setMessage("This is a Dialog")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
        return builder.create();
    }
}

**我的对话Java类**
包com.example.application.example;
公共类ExampleDialog扩展了AppCompatDialogFragment{
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
builder.setTitle(“信息”)
.setMessage(“这是一个对话框”)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(DialogInterface,inti){
}
});
返回builder.create();
}
}

我已经为适配器添加了一个接口。现在从创建此适配器的位置开始,您需要实现接口
ExpandableListAdapterInterFace
按钮Clicked
方法,您可以根据需要更改参数及其类型

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private final ExpandableListAdapterInterFace _expandableListAdapterInterFace;

    interface ExpandableListAdapterInterFace{
        void buttonClicked(String dataHeader,int position);
    }

    //........ your code............

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData, ExpandableListAdapterInterFace expandableListAdapterInterFace) {
        this._context = context;
        this._expandableListAdapterInterFace=expandableListAdapterInterFace;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    //..............your code .........


    @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);
        Button btnListChild = (Button) convertView.findViewById(R.id.btn);
        btnListChild.setOnClickListener(v->{
            _expandableListAdapterInterFace.buttonClicked(childText,childPosition);
        });

        txtListChild.setText(childText);
        return convertView;
    }
   //..................your code............
}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
专用最终可扩展列表适配器接口\u可扩展列表适配器接口;
接口可扩展列表适配器接口{
void按钮点击(字符串数据头,int位置);
}
//……您的代码。。。。。。。。。。。。
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData,ExpandableListAdapter接口ExpandableListAdapter接口){
这._context=context;
这。\ u expandableListAdapterInterFace=expandableListAdapterInterFace;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
//您的代码。。。。。。。。。
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView.findViewById(R.id.lblListItem);
按钮btnListChild=(按钮)convertView.findViewById(R.id.btn);
btnListChild.setOnClickListener(v->{
_expandableListAdapterInterFace.buttonClicked(childText,childPosition);
});
setText(childText);
返回视图;
}
//您的代码。。。。。。。。。。。。
}

感谢您的反馈!由少于1票的人投票