kalmeshwar gurav
kalmeshwar gurav

Reputation: 23

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

My 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;
    }
}

My 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;
}

}

My Stack Trace

-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..

Upvotes: 0

Views: 420

Answers (2)

Iamat8
Iamat8

Reputation: 3906

Get your group name by String in array list at groupPosition you are passing to constructor and you child date by getting getDate() at childPosition of getItems at groupPosition...

On your submit button call like...

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

Upvotes: 0

Aaron He
Aaron He

Reputation: 5549

Seems the mSelections in your adapter already keeps checked positions with some data, but it only adds data to it when check box is checked but doesn't remove the data from it when unchecked.

You have a View.OnClickListener set on the check box, you can get the checked status from ((CheckBox) v).isChecked(). Then update the mSelections accordingly.

Now you can make a public method on the adapter to get a list of selected Group_Items via the mSelection which contains all the checked positions, the same as the indices of the underlying data list.

View.OnClickListener is not ideal. Because check box has its own OnCheckedChangeListener, you don't have to cast the view to a check box to get check status.

Upvotes: 0

Related Questions