1

問題は、スクロール時にチェックボックスの状態を保存することです。下または上にスクロールすると、画面の再描画のために未チェックの項目がチェックされます。誰かが助けてくれれば。アダプターのコードを提供します。

final class ExpAdapter extends CursorTreeAdapter {
        private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();

        LayoutInflater mInflator;
        CursorTreeAdapter cur;

        class ViewHolder {
                    public TextView textView;
                    public CheckBox checkbox;
                }

        public ExpAdapter(Cursor cursor, Context context)
        {
            super(cursor, context);
            mInflator = LayoutInflater.from(context);
        }

        @Override
        protected void bindChildView(View view, Context context, final Cursor cursor,
                boolean isLastChild) {
            //TextView tvChild = (TextView) view.findViewById(android.R.id.text1);

               final TextView tvChild = (TextView) view.findViewById(R.id.label);
                tvChild.setText(cursor.getString(cursor
                        .getColumnIndex(tablename.columname...)));//child

               final CheckBox  chb = (CheckBox)view.findViewById(R.id.chkbox);

                chb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        if (buttonView.isChecked()) {
                            //buttonView.setChecked(true);
                            buttonView.setChecked(true);
                             groupcode = mCuror2.getInt(tablename.columname));
                             levelid = mCuror2.getInt(mCuror2.getColumnIndex(tablename.columname));

                             levelidarray.add(String.valueOf(levelid));
                             groupcodearray.add(String.valueOf(groupcode));


                             String x=(String)tvChild.getText();

                             array.add(x);
                            Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();
                            // do some operations here

                        }
                        else 
                                {
                            buttonView.setChecked(false);
                            //buttonView.setChecked(false);
                            //itemChecked.set(cursor.getPosition(), false);
                            // do some operations here
                            String x = (String)tvChild.getText();

                         levelidarray.remove(String.valueOf(levelid));
                         groupcodearray.remove(String.valueOf(groupcode));
                            array.remove((String)x);

                              Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();

                                }
                    }
                });


           }

       @Override
        protected void bindGroupView(View view, Context context, Cursor cursor,  boolean isExpanded)

           {

               TextView tvGrp = (TextView) view.findViewById(android.R.id.text1);
                tvGrp.setText(cursor.getString(cursor
                        .getColumnIndex(tablename.columname)));

               TextView code = (TextView) view.findViewById(android.R.id.text2);

            }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor)
            {
               int groupId = groupCursor.getInt(groupCursor
                        .getColumnIndex(tablename.columname));

                String filter2 = " LEVEL_ID = " +groupId;
                mCuror2 = dbconnection....,filter2, null, null, null, null);

                return mCuror2;

            }


        @Override
        protected View newChildView(Context context, Cursor cursor,
                boolean isLastChild, ViewGroup parent)
        {

                View mView = mInflator.inflate(R.layout.listviewcheckbox, null);


                 final TextView tvChild = (TextView) mView.findViewById(R.id.label);

                tvChild.setText(cursor.getString(cursor
                        .getColumnIndex(tablename.columname)));

                CheckBox chb = (CheckBox)mView.findViewById(R.id.chkbox);

                chb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        if (buttonView.isChecked()) {
                            //buttonView.setChecked(true);
                            buttonView.setChecked(true);
                             groupcode = mCuror2.getInt(mCuror2.getColumnIndex(tablename.columname));
                             levelid = mCuror2.getInt(mCuror2.getColumnIndex(tablename.columname));

                             levelidarray.add(String.valueOf(levelid));
                             groupcodearray.add(String.valueOf(groupcode));


                             String x=(String)tvChild.getText();

                             array.add(x);
                            Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();
                            // do some operations here

                        }
                        else 
                                {
                            buttonView.setChecked(false);
                            //buttonView.setChecked(false);
                            //itemChecked.set(cursor.getPosition(), false);
                            // do some operations here
                            String x = (String)tvChild.getText();

                         levelidarray.remove(String.valueOf(levelid));
                         groupcodearray.remove(String.valueOf(groupcode));
                            array.remove((String)x);

                              Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();

                                }
                    }
                });

                return mView;


            }

        @Override
        protected View newGroupView(Context context, Cursor cursor,
                boolean isExpanded, ViewGroup parent)
            {   
                View mView = mInflator.inflate(
                        android.R.layout.simple_expandable_list_item_2, null);
                TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1);
                tvGrp.setText(cursor.getString(cursor
                        .getColumnIndex(tablename.columname)));

                TextView code = (TextView)mView.findViewById(android.R.id.text2);
                code.setText(cursor.getString(cursor.getColumnIndex(tablename.columname)));

                return mView;
            }
4

2 に答える 2

0

チェック済みの状態を DB に保存しない場合は、アレイを設定してチェック済みのステータスを追跡し、それを使用してアダプターに設定する必要があります。getChildViewそれを参照するにはグループと子の位置が必要なので、ではなくメソッドで行う必要があると思いますbindChildView

クラス変数を宣言します。

boolean[][] checkedStatus;

コンストラクターに入力します。

public ExpAdapter(Cursor cursor, Context context) {      
    super(cursor, context);      
    mInflator = LayoutInflater.from(context);      
    checkedstatus = new boolean[cursor.getCount()][];                // initializae the first dimension of array
    for (int i=0; i<cursor.getCount(); i++){                         // iterate through the group cursor count
        cursor.moveToPosition(i);
        Cursor childCursor = ...                                     // get the childcursor for group cursor position i 
        checkedStatus[i] = new boolean[childcursor.getCount()];    // initialize the second dimension of array for group position i
        for (int j=0; j<childCursor.getCount(); j++){                // iterate through the childCursor for group position i
            checkedStatus[i][j] = false;                            // initialize the item to not checked for group position i, child position j
        }
    }
}    

次に、それを使用してgetChildView状態を設定します。

CheckBox chb = (CheckBox)mView.findViewById(R.id.chkbox);
if (checkedStatus[groupPosition][childPosition] == true) {
    chb.setChecked(true);
} else {
    chb.setChecked(false);
}

メソッド内の配列も必ず更新してくださいonCheckedChanged

もう1つのオプションは、チェックされたステータスをデータベースに保存することです。その後、それをチェックして、bindView変更時にDBに書き込んで完了できます。もちろん、ペナルティは、チェックボックスをクリックするたびにDBにアクセスして変更を読み書きする必要があるため、通常はこのオプションを避けます。

于 2012-07-04T12:41:30.277 に答える
0

私はResourceCursorTreeAdapterを使用しましたが、私の解決策は次のとおりです。

import android.util.SparseArray;

...

/**
 * A map of children IDs to selection state.
 */
private final SparseArray<Boolean> mSelectedChildrenMap = new SparseArray<Boolean>();

...

@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
    final int _id = cursor.getInt(...); // get ID

    Boolean selected;
    if (mSelectedChildrenMap.get(_id) == null) {
        selected = false;
        mSelectedChildrenMap.put(_id, selected);
    } else
        selected = mSelectedChildrenMap.get(_id);

    /*
     * Checkbox, you can use "view holder" approach...
     */
    CheckBox mCheckBox = (CheckBox) view.findViewById(...);
    mCheckBox.setOnCheckedChangeListener(null);
    mCheckBox.setChecked(selected);
    mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mSelectedChildrenMap.put(_id, isChecked);
        }// onCheckedChanged()
    });

    ...
}// bindChildView()

...

@Override
public void notifyDataSetChanged(boolean releaseCursors) {
    super.notifyDataSetChanged(releaseCursors);
    /*
     * Only clears the map if releaseCursors == true.
     */
    if (releaseCursors)
        synchronized (mSelectedChildrenMap) {
            mSelectedChildrenMap.clear();
        }
}// notifyDataSetChanged()

@Override
public void notifyDataSetInvalidated() {
    super.notifyDataSetInvalidated();
    /*
     * Clears the map.
     */
    synchronized (mSelectedChildrenMap) {
        mSelectedChildrenMap.clear();
    }
}// notifyDataSetInvalidated()

それが役に立てば幸い :-)

于 2012-11-23T05:03:17.570 に答える