3

我正在为 listview 的每个 Iten 实现带有 listview 的复选框。我遇到的问题是,当我单击任何单个复选框时,当我滚动时,其他一些未单击的复选框也被随机单击。我浏览了很多关于 SO 的链接,但它并没有解决我的问题。

public class CustomListViewAdapter extends ArrayAdapter<Client> {

ViewHolder holder = null;
private LayoutInflater inflator = null;
private ArrayList<Client> orderList = null;
ArrayList<View> allViews;

public CustomListViewAdapter(Context context, int resource,
        List<Client> objects) {
    super(context, resource, objects);

    orderList = (ArrayList<Client>) objects;
    inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    allViews = new ArrayList<View>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = inflator.inflate(R.layout.listview_add_order, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
        holder.txtViewName = (TextView) convertView.findViewById(R.id.txtViewAddOrder);
        holder.spinnerAddorder = (Spinner) convertView.findViewById(R.id.spinnerAddOrder);
        holder.checkAddorder = (CheckBox) convertView.findViewById(R.id.checkAddOrder);
        holder.checkAddorder.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();


        holder.checkAddorder.setTag(holder);
    }

    allViews.add(position, holder.spinnerAddorder);

    holder.checkAddorder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            CheckBox cchk = (CheckBox)view;
            ViewHolder checkPosition = (ViewHolder) view.getTag();


            if(cchk.isChecked()) {

                checkPosition.spinnerAddorder.setVisibility(View.VISIBLE);

            } else {
                checkPosition.spinnerAddorder.setVisibility(View.INVISIBLE);
            }
        }
    });

    List<String> list = new ArrayList<String>();
    list.add("Select Quantity");
    list.add("1");
    list.add("2");
    list.add("3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getContext(),R.layout.spinner_add_order_style, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    holder.spinnerAddorder.setAdapter(dataAdapter);

    if(holder.checkAddorder.isChecked()) {


        ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag();
        checkPosition.spinnerAddorder.setVisibility(View.VISIBLE);
    } else {

        ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag();
        checkPosition.spinnerAddorder.setVisibility(View.INVISIBLE);
    }


    Client order = orderList.get(position);

    holder.txtViewName.setText(order.getFirstName());
    return  convertView;
}

private static class ViewHolder {
    public TextView txtViewName = null;
    public Spinner spinnerAddorder = null;
    public CheckBox checkAddorder = null;
}

     }

我的 xml 文件是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="2" >

<TextView
    android:id="@+id/txtViewAddOrder"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1.2"
    android:text="TextView"
    android:textSize="25sp" />

<Spinner
    android:id="@+id/spinnerAddOrder"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".8"
    android:background="@drawable/dropdown" />

<CheckBox
    android:id="@+id/checkAddOrder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp" />

 </LinearLayout>
4

4 回答 4

5

这是由于视图回收而发生的。您需要做的是维护一组说布尔值,并且每次选中复选框时都会切换相应的布尔值。然后在您的 getview 中检查相应的布尔数组位置并设置复选框的状态。

沿着这些思路:我已经对其进行了修改,使其正确显示。尚未检查其余代码的内容。^_^

public class CustomListViewAdapter extends ArrayAdapter<Client> {

ViewHolder holder = null;
private LayoutInflater inflator = null;
private ArrayList<Client> orderList = null;
ArrayList<View> allViews;

boolean[] checkedStates; //********** NEW ********

public CustomListViewAdapter(Context context, int resource,
        List<Client> objects) {
    super(context, resource, objects);

    orderList = (ArrayList<Client>) objects;
    inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    allViews = new ArrayList<View>();

    checkedStates = new boolean[objects.size()]; //********** NEW ********
}

@Override   //********** NEW ******** position argument is now final 
public View getView(final int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = inflator.inflate(R.layout.listview_add_order, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
        holder.txtViewName = (TextView) convertView.findViewById(R.id.txtViewAddOrder);
        holder.spinnerAddorder = (Spinner) convertView.findViewById(R.id.spinnerAddOrder);
        holder.checkAddorder = (CheckBox) convertView.findViewById(R.id.checkAddOrder);
        holder.checkAddorder.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();


        holder.checkAddorder.setTag(holder);
    }

    allViews.add(position, holder.spinnerAddorder);

    holder.checkAddorder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

              checkedStates[position] = !checkedStates[position]; //********** NEW ********
        }
    });

    holder.chechAddorder.setChecked(checkedStates[position]); //********** NEW ********

    List<String> list = new ArrayList<String>();
    list.add("Select Quantity");
    list.add("1");
    list.add("2");
    list.add("3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getContext(),R.layout.spinner_add_order_style, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    holder.spinnerAddorder.setAdapter(dataAdapter);

    if(checkedStates[position]) { //********** NEWEST ******** assuming true is checked and false is unchecked


        ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag();
        checkPosition.spinnerAddorder.setVisibility(View.VISIBLE);
    } else {

        ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag();
        checkPosition.spinnerAddorder.setVisibility(View.INVISIBLE);
    }


    Client order = orderList.get(position);

    holder.txtViewName.setText(order.getFirstName());
    return  convertView;
}

private static class ViewHolder {
    public TextView txtViewName = null;
    public Spinner spinnerAddorder = null;
    public CheckBox checkAddorder = null;
}

     }
于 2013-04-27T06:21:17.007 回答
1

只需设置可聚焦属性 = false。在适配器中的 CheckBox 上制作 onClickListener。

{ <CheckBox
    android:id="@+id/cb_alert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dip"
    android:button="@drawable/custom_checkbox"
    android:checked="true"
    android:focusable="false"
    android:padding="7dip" />}
于 2015-09-06T10:38:40.747 回答
0

使用双向数据绑定来处理复选框状态 https://developer.android.com/topic/libraries/data-binding/two-way#kotlin

<CheckBox
android:id="@+id/rememberMeCheckBox"
android:checked="@={viewmodel.rememberMe}"

/>

class LoginViewModel : BaseObservable { // val data = ...

@Bindable
fun getRememberMe(): Boolean {
    return data.rememberMe
}

fun setRememberMe(value: Boolean) {
    // Avoids infinite loops.
    if (data.rememberMe != value) {
        data.rememberMe = value

        // React to the change.
        saveData()

        // Notify observers of a new value.
        notifyPropertyChanged(BR.remember_me)
    }
}

}

于 2019-12-11T16:31:44.307 回答
0

Override this methods in your adapter:

override fun getItemId(position: Int) = position.toLong()

override fun getItemViewType(position: Int) = position
于 2020-05-05T22:43:32.753 回答