1

チェックボックスをオンにすると、コードは同じ行にあるテキストビューの文字列のみを返します。

LayoutInflater と、文字列を含むリストを作成するためのコードを持つ別のクラスを使用しています。

私のコード:

 public class InteractiveArrayAdapter extends ArrayAdapter<model> {


private final List<model> list;
  private final Activity context;

  public InteractiveArrayAdapter(Activity context, List<model> list) {
    super(context, R.layout.rep, list);
    this.context = context;
    this.list = list;

  }


  static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
      }

  public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
          LayoutInflater inflator = context.getLayoutInflater();
          view = inflator.inflate(R.layout.rep, null);
          final ViewHolder viewHolder = new ViewHolder();
          viewHolder.text = (TextView) view.findViewById(R.id.TextView07);
          viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox05);
          viewHolder.checkbox
              .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                  model element = (model) viewHolder.checkbox
                      .getTag();
                  element.setSelected(buttonView.isChecked());

                }
              });
          view.setTag(viewHolder);
          viewHolder.checkbox.setTag(list.get(position));
        } else {
          view = convertView;
          ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;


      }
}

私のxml:

  <?xml version="1.0" encoding="utf-8"?>
  <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
android:id="@+id/h103"
android:layout_width="match_parent"
android:layout_height="27dp"
android:layout_x="0dp"
android:layout_y="0dp"
android:background="@drawable/back1" >

<TableRow
    android:id="@+id/TableRow05"
    android:layout_width="match_parent"
    android:layout_height="27dp"
    android:layout_marginTop="4dp" >

    <CheckBox
        android:id="@+id/CheckBox05"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="-4dp"
        android:button="@drawable/test" />

    <TextView
        android:id="@+id/TextView07"
        android:layout_width="220dp"
        android:layout_height="22dp"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="-6dp"
        android:text="" />

    <TextView
        android:id="@+id/TextView08"
        android:layout_width="110dp"
        android:layout_height="22dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="-4dp"
        android:text="095110263" />

</TableRow>

</TableLayout>

</AbsoluteLayout>
4

2 に答える 2

0

最初に ur リスト ビューの下部にある Add 1 ボタン btnOk

btnOk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                 StringBuffer responseText = new StringBuffer();


                 ArrayList<model> almodel = adapter.almodel;
                 responseText.append("The following were selected...\n");

                 for(int i=0;i<almodel.size();i++)
                 {
                     model m = almodel.get(i);
                     if(m.isSelected())
                     {

                      responseText.append("\n" + m.getName());

                     }

                  }


         Toast.makeText(getApplicationContext(),responseText,Toast.LENGTH_LONG).show();
于 2013-03-08T09:55:51.237 に答える
0

リストビューで使用すると自動的に呼び出され、チェックされたチェックボックスが非表示になるため、checkbox.onCheckedChangedではなくcheckbox.onclicklistenerで作業する必要があります。



    new OnClickListener()
    {
         @Override
         public void onClick(View view)
         {}
    }


onclicklistener では、チェック ボックスであるパラメーターとしてビューを取得します。ここでビューには、チェックボックスが設定されているビューを返す getparent() のメソッドがあります。ログを印刷することで確認できます。チェックボックスの親を取得した場合、1つの子ビューがチェックボックスである親のすべての子ビューも取得できます。すべての子ビューから、トーストに表示する値を取得できます。チェックボックスの親がリスト項目のビューに別の親を持っている場合、getparent() メソッドを呼び出して親ビューの親を取得することもできます。

ビューには親子関係があることに注意してください。すべての子が必要な場合は、親を取得する必要があります。

于 2013-03-07T05:12:24.557 に答える