0

各行にチェックボックスがあるレイアウトを持つリスト フラグメントを含むアクティビティがあります。onclickチェックボックスにxml属性を設定し、テストのために次のことを行います

public void onBoxClick(View v){
    checkedItems = listview.getCheckedItemPositions();
    int checkedItemsCount = checkedItems.size();

}

checkedItemsCount戻ってきて0、あなたが使用しているチェックされているアイテムを取得しようと思ったのですlistview.getCheckedItemPositions()が、そうではありません。リストでチェックされているものをどのように知ることができますか?

これは私のリストフラグメントの作成です

@Override
    public void onActivityCreated(Bundle state){
        super.onActivityCreated(state);
        listview = getListView();
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listview.setItemsCanFocus(false);
        setEmptyText("No Bowlers");       
        registerForContextMenu(getListView());
        populateList();
    }
4

2 に答える 2

0

This post might help. It gives a solution using a custom ResourceCursorAdapter, which provides a CheckBox and a TextView for each ListView row.

To select multiple items in ListView, check this page. Note that the example uses a ListActivity instead of a ListFragment, but your code will end up being extremely similar. Just make sure you implement the Fragment lifecycle methods correctly (i.e. setting the Adapter in the ListFragment's onActivityCreated(), etc.).

于 2012-01-16T18:18:13.830 に答える
0

Custom Adapter's で問題を回避し、変数bindViewを作成しましたArrayList<Integer>

ArrayList<Integer> mCheckedItems = new ArrayList<Integer>();

そして、チェックボックスにbindViewa を設定しcheckedchangelistenerて、ボックスがチェックされているかどうかを確認します。チェックされている場合は、カーソルが取得したデータベースのIDを入力しますmCheckedItems Array

アダプタ:

public class CheckAdapter extends SimpleCursorAdapter{

    Context context;

    public CheckAdapter(Context context, int layout, Cursor c,String[] from, int[] to,int flag) {
        super(context, layout, c, from, to);
        this.context = context;
    }

    @Override
    public void bindView(View view,Context context,Cursor cursor){
        final String name = cursor.getString(cursor.getColumnIndex(BowlersDB.NAME));
        final int id = cursor.getInt(cursor.getColumnIndex(BowlersDB.ID));
        TextView tv = (TextView)view.findViewById(R.id.nameCheckTV);
        tv.setText(name);

        CheckBox cb = (CheckBox)view.findViewById(R.id.checkBox1);
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

                if(isChecked){
                    mCheckedItems.add(id);
                }else if(!isChecked){
                    for(int i=0; i< mCheckedItems.size(); i++){
                        if(mCheckedItems.get(i) == id){
                            mCheckedItems.remove(i);
                        }
                    }                   
                }

            }

        });
    }

IDが配列に挿入された後、配列リストを使用して必要な方法でそれらを使用しました

于 2012-01-18T04:26:37.537 に答える