Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

android - check box in listview not working properly

I am implementing the checkbox with listview for every Iten of listview.The Problem i am getting is when I am clicking on any single check box and when i scroll then some other checkboxes which are not clicked also getting clicked randomly. i went through many link on SO but it did'nt solve my problem.

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;
}

     }

my xml file is

<?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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is happening because of view recycling. What you need to do is maintain an array of say booleans and every time a checkbox is checked toggle the corresponding boolean. Then in your getview check the corresponding boolean array position and set the state of the check box.

Along these lines: I have modified it so it displays correctly. Have not checked what the rest of your code is upto. ^_^

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;
}

     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...