想要在单击提交按钮时从 ExpandableListView 获取选中复选框的标题和日期

Want to get title and date of checked CheckBox from ExpandableListView when Click on submit Button

I created ExpandableListView with titles and checkBox as GroupItem and Due Date and date as child Item..

I want to get Only _title_(Monthly Return Filing in Form 10) and _date_(15th of Next Month) of checked checkBoxes when user select them and click on submit button

我的 BaseExpandableListAdapter

@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    Group_Items gr = (Group_Items) getGroup(groupPosition);
    long group_id = getGroupId(groupPosition);

    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.expandable_group_items, null);

        // adding onClick listener on CheckBox for the very first time
        chk = (CheckBox) convertView.findViewById(R.id.chk);
        chk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                mSelections.put(position, !mSelections.get(position, false));
                ((CheckBox) v).setChecked(mSelections.get(position, false));
            }
        });
    }

    final TextView title = (TextView) convertView.findViewById(R.id.title);
    title.setText(gr.getName());
    CheckBox chk = (CheckBox) convertView.findViewById(R.id.chk);
    chk.setTag(groupPosition);

    // reassigning checkbox to its ticked state
    chk.setChecked(mSelections.get(groupPosition, false));
    });
    if (groupPosition==7){
        chk.setVisibility(View.GONE);
        return convertView;
    }else {
        chk.setVisibility(View.VISIBLE);
        return convertView;
    }
}

我的FirstRow.java

public class FirstRow extends AppCompatActivity {
private MyBaseExpandableListAdapterFirstRow ExpAdapter;
private ArrayList<Group_Items> ExpListItems;
private ExpandableListView ExpandList;
Child_Items ch;
Button submit_info;
Toolbar mToolbar;

@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expandable_listview);
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);
    
    alertDialogmsg();

    ExpandList = (ExpandableListView) findViewById(R.id.exp_lv);
    ExpListItems = setAllData();
    ExpAdapter = new MyBaseExpandableListAdapterFirstRow(FirstRow.this, ExpListItems);
    ExpandList.setAdapter(ExpAdapter);

    submit_info= (Button) findViewById(R.id.submit_info);
    submit_info.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

private void alertDialogmsg() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Select Taxes");
    alertDialog.setMessage("Please select applicable section only to avoid unnecessary reminders");
    alertDialog.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
    alertDialog.show();
}

private ArrayList<Group_Items> setAllData() {

    String group_titles[] = {
            "Payment of Employer and employees contribution",
            "Monthly Return Filing in Form 5 and Declaration in form 2",
            "Monthly Return Filing in Form 10",
            "Monthly Return Filing in Form 12",
            "Statement of Recovery from contractor",
            "Annual Return in Form 6A",
            "Submission of contribution card of employees leaving services",
            "Renewal of contribution card of an employee by filing Form 3 & 3A"};

    String child_info[] = {
            "We have deployed secured web services framework in accordance with OASIS standards. Please refer the user manual (Secured Web service user manual) under the help section and do the necessary changes at your end to start consuming the same. Existing web services shall be discontinued w.e.f 1-Dec-2015",
            "Return of employees qualifying for membership",
            "Return of members leaving the services during the month",
            "Statement of contribution",
            "Self Explnatory",
            "Consolidated annual contribution statement",
            "Self Explnatory",
            "Self Explnatory"};

    String dates[] = {
            "15th of Next Month + 5 Days Grace period",
            "15th of Next Month",
            "15th of Next Month",
            "25th of Next Month",
            "7th of Next Month",
            "30th April",
            "20th of Next Month",
            "Within one month on the expiry of contribution card currancy to the commissioner"};

    ArrayList<Group_Items> list = new ArrayList<Group_Items>();
    ArrayList<Child_Items> ch_list= new ArrayList<Child_Items>();
    ch = new Child_Items();
    int i=0;
    int size=1;

    for (String group_title : group_titles) {
        Group_Items gru = new Group_Items();

        gru.setName(group_title);

        ch_list = new ArrayList<Child_Items>();
        for (; i < size; i++) {
            Child_Items ch = new Child_Items();

            ch.setChild_title(child_info[i]);
            ch.setDd("Due Date:");
            ch.setDate(dates[i]);

            ch_list.add(ch);
        }

        gru.setItems(ch_list);
        list.add(gru);
        size=size+1;
    }
    return list;
}

}

我的堆栈跟踪

-14 13:29:13.985 26489-26489/neoxsoft.com.taxmukti E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: neoxsoft.com.taxmukti, PID: 26489
                                                                   java.lang.NullPointerException
                                                                       at neoxsoft.com.taxmukti.allsubtaxes_first.FirstRow.MyBaseExpandableListAdapterFirstRow.getChildView(MyBaseExpandableListAdapterFirstRow.java:64)
                                                                       at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
                                                                       at android.widget.AbsListView.obtainView(AbsListView.java:2318)
                                                                       at android.widget.ListView.makeAndAddView(ListView.java:1824)
                                                                       at android.widget.ListView.fillDown(ListView.java:717)
                                                                       at android.widget.ListView.fillSpecific(ListView.java:1375)
                                                                       at android.widget.ListView.layoutChildren(ListView.java:1642)
                                                                       at android.widget.AbsListView.onLayout(AbsListView.java:2139)
                                                                       at android.view.View.layout(View.java:14846)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:4664)
                                                                       at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
                                                                       at android.view.View.layout(View.java:14846)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:4664)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                       at android.view.View.layout(View.java:14846)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:4664)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1697)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1551)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1460)
                                                                       at android.view.View.layout(View.java:14846)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:4664)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                       at android.view.View.layout(View.java:14846)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:4664)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1697)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1551)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1460)
                                                                       at android.view.View.layout(View.java:14846)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:4664)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                       at android.view.View.layout(View.java:14846)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:4664)
                                                                       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2010)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1767)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1018)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5756)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                       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:5421)
                                                                       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:979)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
                                                                       at dalvik.system.NativeStart.main(Native Method)

I took submit_info button in my FirstRow.java and want to get values of selected CheckBox when click on submit button with onClickListener So, guys how to get values of the same please help me..

您的适配器中的 mSelections 似乎已经保留了一些数据的选中位置,但它仅在选中复选框时才向其添加数据,但在未选中时不会从中删除数据。

您在复选框上设置了 View.OnClickListener,您可以从 ((CheckBox) v).isChecked() 获取选中状态。然后相应地更新 mSelections

现在您可以在适配器上创建一个 public 方法,通过 mSelection 获取选定的 Group_Items 列表,其中包含所有选中的位置,与索引相同基础数据列表。

View.OnClickListener 并不理想。因为复选框有自己的 OnCheckedChangeListener,所以您不必将 view 强制转换为复选框来获取检查状态。

通过 StringgroupPosition 的数组列表中获取您的组名,您将传递给构造函数,并通过在 childPosition 获取 getDate() 来 child 日期getItemsgroupPosition...

在您的提交按钮上调用...

String name = group_al.get(groupPosition).getName();
String date = group_al.get(groupPosition).getItems().get(childPosition).getDate();