1

我有一个带有复选框的列表视图。我想从列表视图中获取所选复选框的所有文本。因为,我是 android 新手,我参考了一些使用视图持有者但我没有使用的链接。

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)

我在这里得到了结构。任何答案都会有帮助

4

5 回答 5

2

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

selectedString = new ArrayList<String>();

或调用 setSelectedString 函数。

于 2013-10-22T06:46:06.040 回答
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
            }
       }
  });

如果您需要任何其他信息,请告诉我。

于 2013-10-22T07:04:50.213 回答
1

你没有设置对象selectedString 首先在你的Adapetr类中初始化它或直接从你的Activity.

在您的 Activity 类更改代码中,像这样..

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

并调用此方法将所选字符串数据列表获取到您的活动..

checkBoxListViewAdapter.getSelectedString();
于 2013-10-22T06:47:47.567 回答
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);
}
});

还与适配器中的 converview.setonClicklistener 一起使用。

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

于 2013-10-22T06:48:54.743 回答
1

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

于 2013-10-22T06:51:58.820 回答