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

时间:2013-10-22 11:23:53

标签: java android listview android-checkbox

我正在使用arraylist并在listview中显示值,现在我想要所选文本。我试过但我无法完成它。这是我的代码,我希望所选文本存储在arraylist中。

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

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


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

                    System.out.println("hi hi" +a);
                }
            });        


    }
    }

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;
    }
    static class Holder {
        CheckBox cbxSelected; 
        }
    LayoutInflater inflater = null;

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

         selectedString = new ArrayList<String>();
        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 row, ViewGroup parent) {
Context context = null;
        Holder holder = null;
        View vi = row;
        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            row = inflater.inflate(R.layout.ec_checkbox_number, null);

            holder = new Holder();
            holder.cbxSelected = (CheckBox)row.findViewById(R.id.checkBox);
            row.setTag(holder);
        }
        else {
            holder = (Holder)row.getTag();
            holder.cbxSelected.setOnCheckedChangeListener(null);
        }

        holder.cbxSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked) {
                    selectedString.add("k");
                }
                else {
                    // do something
                }
                //cbxZoneChecked.setChecked(CurrentContext.RatingItems.Items.contains(ratingChoosenItems.get(position)));

            }
        });


        return vi;
    }


}

我知道如何完成此任务。任何答案都会有所帮助

error is,10-23 13:18:42.900: E/AndroidRuntime(2901): FATAL EXCEPTION: main
10-23 13:18:42.900: E/AndroidRuntime(2901): java.lang.NullPointerException
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.view.LayoutInflater.from(LayoutInflater.java:211)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at com.bambeeq.conferencecall.CheckBoxListViewAdapter.getView(EcConferenceNumber.java:113)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.AbsListView.obtainView(AbsListView.java:2472)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.ListView.makeAndAddView(ListView.java:1775)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.ListView.fillDown(ListView.java:678)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.ListView.fillFromTop(ListView.java:739)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.ListView.layoutChildren(ListView.java:1628)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.AbsListView.onLayout(AbsListView.java:2307)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.view.View.layout(View.java:14107)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.view.View.layout(View.java:14107)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.view.View.layout(View.java:14107)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.view.ViewGroup.layout(ViewGroup.java:4655)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
10-23 13:18:42.900: E/AndroidRuntime(2901):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)

2 个答案:

答案 0 :(得分:1)

首先,您必须维护已选中的复选框状态。之后你必须将setonclicklistener放入复选框而不是setoncheckedchangedlistener,并在onclick方法中检查是否选中了复选框,然后将文本放入arraylist,否则不会。

有关详细信息,请查看此link

答案 1 :(得分:0)

您应该在适配器中定义Holder类

static class Holder {
CheckBox cbxSelected; 
}

在getView()

中初始化它
Holder holder = null;

要从持有者读取值,请在getView()

中添加
if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            row = inflater.inflate(R.layout.ec_checkbox_number, null);

            holder = new Holder();
            holder.cbxSelected = (CheckBox)row.findViewById(R.id.cbxChecked);
            row.setTag(holder);
        }
        else {
            holder = (Holder)convertView.getTag();
            holder.cbxSelected.setOnCheckedChangeListener(null);
        }

还在getView中添加CheckBox侦听器:

holder.cbxSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked) {
                    //do somethind
                }
                else {
                    // do something
                }
                //cbxZoneChecked.setChecked(CurrentContext.RatingItems.Items.contains(ratingChoosenItems.get(position)));
                CashWork.clearRatioCash();
            }
        });