0

嗨,我在我的片段中使用可扩展列表视图,在这里当我单击可扩展列表视图中的复选框时,我想以数组格式获取主要活动的值。但是每当我点击时应用程序就会崩溃checkbox

可扩展列表适配器

package com.example.limitscale.beautylog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
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.LinearLayout;
import android.widget.TextView;
import com.example.limitscale.beautylog.model.BookedInfo;

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;
ArrayList<BookedInfo> selectedarray = new ArrayList<BookedInfo>();
CheckBox check;

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);
    String valamt[]  = childText.split(",");

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    final LinearLayout content=(LinearLayout) convertView.findViewById(R.id.content);
    final TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
    final TextView price = (TextView) convertView.findViewById(R.id.aMount);
    check=(CheckBox) convertView.findViewById(R.id.checkBox);
    txtListChild.setText(valamt[0]);
    price.setText("Rs" + valamt[1]);

    check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                BookedInfo bookedInfo = new BookedInfo();
                String check = "1";
                bookedInfo.setServiceName(txtListChild.toString());
                bookedInfo.setmServicePrice(price.toString());
                bookedInfo.setmBrandQty(check);
                selectedarray.add(bookedInfo);
                ((MainActivity) _context).checked(selectedarray);
            }
        }
    });

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

    return convertView;
}

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

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

这是我的日志猫

05-20 12:27:21.715  30353-30353/com.example.limitscale.beautylog E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.limitscale.beautylog, PID: 30353
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.limitscale.beautylog.MainActivity
        at com.example.limitscale.beautylog.ExpandableListAdapter$1.onCheckedChanged(ExpandableListAdapter.java:73)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
        at android.widget.CompoundButton.toggle(CompoundButton.java:87)
        at android.widget.CompoundButton.performClick(CompoundButton.java:99)
        at android.view.View$PerformClick.run(View.java:18491)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5336)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:873)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
        at dalvik.system.NativeStart.main(Native Method)

实际上,当我将其作为单独的项目进行测试时,相同的代码运行良好,但是当我合并到我的主项目中时,单击复选框时应用程序崩溃了。

4

4 回答 4

0

首先将其转换为活动:

Activity activity = (Activity) context;

然后使用:

((MainActivity) activity).checked(selectedarray);
于 2016-05-20T07:20:59.853 回答
0

请在 getView 中定义您的复选框

final CheckBox check=(CheckBox) convertView.findViewById(R.id.checkBox);

如果您公开定义它,那么您只会获得最后一个复选框值

于 2016-05-20T07:05:36.313 回答
0

您正在使用应用程序上下文进行实例化ExpandableListAdapter,这就是为什么您不能将其转换为 Activity (这就是您的错误所说的)。你一定在new ExpandableListAdapter(getApplicationContext(),...);某个地方有类似的东西。如果您在 上执行此操作MainActivity,则必须将该行更改为new ExpandableListAdapter(MainActivity.this,...);

于 2016-05-20T07:31:40.687 回答
0

遵循代码中的某个版本,让我知道它是否正常工作。

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

并在检查更改中,以下更改

check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            BookedInfo bookedInfo = new BookedInfo();
            String check = "1";
            bookedInfo.setServiceName(txtListChild.toString());
            bookedInfo.setmServicePrice(price.toString());
            bookedInfo.setmBrandQty(check);
            selectedarray.add(bookedInfo);
            ((MainActivity) activity).checked(selectedarray);
        }
    }
});
于 2016-05-20T07:13:47.690 回答