从android中的listview获取所选复选框的文本?

时间:2013-10-22 06:40:19

标签: android listview checkbox baseadapter android-checkbox

我有一个带有checkBox的List视图。我想从列表视图中获取所选复选框的所有文本。因为,我是android的新手我引用一些使用视图持有者的链接,但我没有使用。< / p>

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


}

我在选中复选框时收到错误。我想存储所有选中的复选框文本。

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>

错误日志

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)

我在这里得到了结构。任何答案都将是完整的

5 个答案:

答案 0 :(得分:2)

你没有初始化selectedString - 你需要初始化它 - 当onchange事件被触发时它为null,在构造函数中初始化它。

selectedString = new ArrayList<String>();

或调用setSelectedString函数。

答案 1 :(得分:1)

您没有设置对象selectedString 首先在Adapetr课程中对其进行初始化,或直接从Activity

进行设置

在您的Activty类中更改此代码..

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

并调用此方法将所选字符串数据列表添加到您的活动中。

checkBoxListViewAdapter.getSelectedString();

答案 2 :(得分:1)

使用这种方式:

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

也可以在Adapter中使用converview.setonClicklistener。

只需更改CheckBox cb =(CheckBox)converview.findViewById(R.id.checkbox_id);

答案 3 :(得分:1)

使用new运算符初始化字符串数组'myStrings'或创建字符串列表。

答案 4 :(得分:1)

由于您使用自定义listview,您可以通过以下方式实现。在适配器类的getView方法中使用oncheckChangedListener。

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

如果您需要任何其他信息,请与我们联系。