0

我正在使用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)
4

2 回答 2

1

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

欲了解更多信息,请查看此链接

于 2013-10-22T17:30:37.677 回答
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();
            }
        });
于 2013-10-22T11:43:35.473 回答