0

我有一个列表视图,我希望在其上使用复选框进行多项选择,但是当我添加 android:choiceMode="multipleChoice"到我的 XML 文件时,对复选框的检查并不方便。

需要双击 ListView 中的一行才能显示,单击它会再次消失。

它可以在没有的情况下使用android:choiceMode="multipleChoice",但是我将无法使用 ListView。

getCheckedItemPositions()

任何人都知道为什么会这样?

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/nameList"
        android:choiceMode="multipleChoice"
        tools:listitem="@android:layout/simple_list_item_checked"
        android:layout_centerHorizontal="true" android:layout_alignParentTop="true"
        android:layout_above="@+id/button"/>

这是 Activity 类中的代码。

listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, stringArray);
    playerList = (ListView)this.findViewById(R.id.nameList);
    playerList.setAdapter(listAdapter);

    //onClickListener
    playerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            CheckedTextView checkedTextView = (CheckedTextView) view;
            checkedTextView.toggle();
        }
    });

请帮助我.. 提前谢谢

4

1 回答 1

1

请检查以下代码。您的适配器类将如下所示。

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {
    Context ctx;
    LayoutInflater lInflater;
    ArrayList<Position> arraylist;

    ListAdapter(Context context, ArrayList<Position> products) {
        ctx = context;
        arraylist = products;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return arraylist.size();
    }

    @Override
    public Object getItem(int position) {
        return arraylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.item, parent, false);
        }

        Position pos = getselectedposition(position);
        ((TextView) view.findViewById(R.id.Textview1)).setText(""+pos.position);
        CheckBox chkbox = (CheckBox) view.findViewById(R.id.cbBox);
        chkbox.setOnCheckedChangeListener(myCheckChangList);
        chkbox.setTag(position);
        chkbox.setChecked(pos.ischeckedflag);
        return view;
    }

    Position getselectedposition(int position) {
        return ((Position) getItem(position));
    }

    ArrayList<Position> getcheckedposition() {
        ArrayList<Position> checkedposition = new ArrayList<Position>();
        for (Position p : arraylist) {
            if (p.ischeckedflag)
                checkedposition.add(p);
        }
        return checkedposition;
    }

    OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            getselectedposition((Integer) buttonView.getTag()).ischeckedflag = isChecked;
        }
    };
}

请从以下链接下载源代码。

https://github.com/ItsRajesh4uguys/ListView_with_Checkboxes_Android

希望这会帮助你。

于 2013-06-01T09:34:41.253 回答