0

私はarraylistを使用していて、リストビューに値を表示しています。選択したテキストが必要です。試しましたが、完了できません。これは私のコードです。選択したテキストを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;
    }


}

私はこれを完了する方法を知っています.Any答えが役に立ちます

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

0

アダプターでホルダークラスを定義する必要があります

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 に答える