1

I am having a List view with checkBox .I want to get all the text of selected check Box from the list view .Since ,I am new to android I refer some links which uses view holder but i am not using .

public class EcConferenceNumber extends Activity{
    ListView checkBoxNumberListView;
    ConferenceAdapter adapter;
    Button doneBtn,cancelBtn;
    EditText profileName;
    CheckBoxListViewAdapter cbAdapter;
    static ArrayList<String> checkBoxData = null;
    String[]  myStrings;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ec_number_selection);   
        adapter = new ConferenceAdapter(this);
        checkBoxNumberListView = (ListView) findViewById(R.id.numberselectionlistView);

        doneBtn=(Button) findViewById(R.id.donenumberbutton);
        cancelBtn=(Button) findViewById(R.id.cancelnumberbutton);
        Intent intent = getIntent();
        myStrings = intent.getStringArrayExtra("strings");
    /*ArrayList<String> got=    cbAdapter.getSelectedString();

    System.out.println(""+got);*/

         checkBoxData=new ArrayList<String>(Arrays.asList(myStrings));

        //checkBoxNumberListView.setAdapter(new CheckBoxListViewAdapter(EcConferenceNumber.this, adapter, checkBoxData));

        //System.out.println(""+checkBoxData);
          final CheckBoxListViewAdapter checkBoxListViewAdapter = new CheckBoxListViewAdapter(
                    EcConferenceNumber.this, adapter, checkBoxData);
                    checkBoxListViewAdapter.setSelectedString(new ArrayList<String>());
                    checkBoxNumberListView.setAdapter(checkBoxListViewAdapter);

            doneBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    ArrayList<String>   a=    checkBoxListViewAdapter.getSelectedString();
                    for(String b:a)
                    System.out.println("hi hi" +b);
                }
            });     


    }
    }

class CheckBoxListViewAdapter extends BaseAdapter {
    private List<String> calendars = null;

    ArrayList<String> selectedString;
    public ArrayList<String> getSelectedString() {
        return selectedString;
    }

    public void setSelectedString(ArrayList<String> selectedString) {
        this.selectedString = selectedString;
    }

    LayoutInflater inflater = null;

    public CheckBoxListViewAdapter(Activity activity, ConferenceAdapter adapter, ArrayList<String> checkBoxData) {
        this.calendars = checkBoxData;

        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return calendars.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
final Context c = null;

        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.ec_checkbox_number, null);

        TextView cbTV = (TextView) vi.findViewById(R.id.checkboxtextView);
        final CheckBox checkBox = (CheckBox) vi.findViewById(R.id.checkBox);

        cbTV.setText(calendars.get(position));


        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectedString.add(checkBox.getText().toString());
                }else{
                    selectedString.remove(checkBox.getText().toString());
                }


            }

        });

        return vi;
    }


}

I am getting error while selecting the checkbox .I want to store all the selected check box text.

ec_number_selection.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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/meetingprofilename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="Profile Name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/meetingprofilename"
        android:layout_alignBottom="@+id/meetingprofilename"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/meetingprofilename"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/cancelnumberbutton"
        android:layout_width="158dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/editText2"
        android:text="Cancel" />

    <Button
        android:id="@+id/donenumberbutton"
        android:layout_width="158dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/cancelnumberbutton"
        android:text="Done" />

    <ListView
        android:id="@+id/numberselectionlistView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/cancelnumberbutton"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1" >

    </ListView>

</RelativeLayout>

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

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:text="" />

    <TextView
        android:id="@+id/checkboxtextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox"
        android:layout_alignBottom="@+id/checkBox"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="107dp"
        android:text="TextView" />

</RelativeLayout>

error log

10-22 12:03:27.950: E/AndroidRuntime(25520): FATAL EXCEPTION: main
10-22 12:03:27.950: E/AndroidRuntime(25520): java.lang.NullPointerException
10-22 12:03:27.950: E/AndroidRuntime(25520):    at com.bambeeq.conferencecall.CheckBoxListViewAdapter$1.onCheckedChanged(EcConferenceNumber.java:109)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.widget.CompoundButton.setChecked(CompoundButton.java:137)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.widget.CompoundButton.toggle(CompoundButton.java:92)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.widget.CompoundButton.performClick(CompoundButton.java:104)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.view.View$PerformClick.run(View.java:17420)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.os.Handler.handleCallback(Handler.java:615)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.os.Looper.loop(Looper.java:137)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at android.app.ActivityThread.main(ActivityThread.java:4944)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at java.lang.reflect.Method.invoke(Method.java:511)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
10-22 12:03:27.950: E/AndroidRuntime(25520):    at dalvik.system.NativeStart.main(Native Method)

I got struct here.Any answer will be help full

user2841300
  • 353
  • 1
  • 7
  • 23
  • http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button. check this if it helps – Raghunandan Oct 22 '13 at 06:41

5 Answers5

2

your didn't init selectedString - you need to initialize it - it is null when the onchange event is triggered, init it in the constructor.

selectedString = new ArrayList<String>();

or call the setSelectedString function.

בועז יערי
  • 445
  • 1
  • 5
  • 14
1

You are not settimg the object selectedString First initialize it in your Adapetr class or directly set it from your Activity.

IN your Activty class change code like this..

    CheckBoxListViewAdapter checkBoxListViewAdapter = new CheckBoxListViewAdapter(
    EcConferenceNumber.this, adapter, checkBoxData);
    checkBoxListViewAdapter.setSelectedString(new ArrayList<String>());
    checkBoxNumberListView.setAdapter(checkBoxListViewAdapter);

And call this method to get the selected string data list to your activity..

checkBoxListViewAdapter.getSelectedString();
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • i have created new object for Arraylist inside getview() is it correct? – user2841300 Oct 22 '13 at 06:55
  • Dont try to initilize in the getview() because it calls multiple times try init in your adapter constructor.then there is only object.. – kalyan pvs Oct 22 '13 at 07:01
  • i want to use this selectedString in EcSelectedNumber.java class how can i do that? – user2841300 Oct 22 '13 at 07:02
  • i have updated your code where can i get the values of selected check boxes – user2841300 Oct 22 '13 at 07:45
  • it is returning null values ya..doneBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub ArrayList a= checkBoxListViewAdapter.getSelectedString(); for(String b:a) System.out.println("hi hi" +b); } }); – user2841300 Oct 22 '13 at 07:59
1

use this way :

  mListView.setOnItemClickListener(new OnItemClickListener()
  {
   @Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
    CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox_id);
}
});

also use with converview.setonClicklistener in Adapter.

just change CheckBox cb = (CheckBox) converview.findViewById(R.id.checkbox_id);

Shivang Trivedi
  • 2,182
  • 1
  • 20
  • 26
1

initialize string array 'myStrings' with new operator or create a string list.

Sino
  • 886
  • 7
  • 21
1

Since you are using custom listview you can achieve in the following manner.Use oncheckChangedListener in the getView method of adapter class.

myCheck.setOncheckedChangeListener(new OnCheckchangedListener(){
           public void onCheckedChanged(CompoundButton btn,boolean isChecked)
    {
          if(isChecked){
               // here you can get the text of selected check box
            }
       }
  });

Let me know if you need any other info.

Akshay
  • 2,506
  • 4
  • 34
  • 55