Checkbox check repeats in listview

不想你离开。 提交于 2020-01-06 02:30:26

问题


I have the following implementation of a custom ListView populated using a CursorAdapter:

private class CurAdapter extends CursorAdapter{

        public CurAdapter(Context context, Cursor c, int flags) {

            super(context, c, flags);

        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            final ViewHolder holder = (ViewHolder) view.getTag();

            String name = (cursor.getString(cursor.getColumnIndexOrThrow("NotificationDateFor")));
            String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));



            holder.nametext.setText(name);
            setImage(image, holder.iv); 
            holder.chk.setOnClickListener(onchk);


        } 
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View view = LayoutInflater.from(context).inflate(R.layout.group_list, null);

            ViewHolder holder = new ViewHolder(view);
            view.setTag(holder);
            return view;

        }

        private View.OnClickListener onchk = new View.OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        };


        public class ViewHolder {
            TextView nametext;
            RoundedImageView iv; 
            CheckBox chk; 

            public ViewHolder(View view){

                iv = (RoundedImageView)view.findViewById(R.id.imageView2);
                nametext = (TextView) view.findViewById(R.id.textView1);
                chk = (CheckBox) view.findViewById(R.id.checkBox1);

            }



        }

    }

I am using a Holder pattern to avoid repetition, but I get check-boxes checked at random while scrolling my list, what do I miss here?

Also I want to know how to store the values of clicked checkboxes in my onClick of the checkbox.


回答1:


Just create an android project and do this step by step:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="showDialog"
    android:text="Click Here..." />

<TextView
    android:id="@+id/techView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java

package com.example.multichoiceitem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

final String[] items = { ".NET", "J2EE", "PHP", "Android", "DBMS", "JAVA",
        "C", "C++" };
String[] itemstemp;
boolean[] selected = new boolean[items.length];
String selectedTech = "Selected Tech - ";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    itemstemp = new String[items.length];
    itemstemp[0] = ".NET";
    itemstemp[1] = "Android";
    for (int i = 2; i < items.length; i++) {
        itemstemp[i] = "";
    }
    for (int i = 0; i < items.length; i++) {
        for (int j = 0; j < itemstemp.length; j++) {
            if (items[i].equals(itemstemp[j])) {
                selected[i] = true;
            }
        }
    }
}

public void showDialog(View v) {
    selectedTech = "";
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick a Language");

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            for (int i = 0; i < items.length; i++) {
                if (selected[i]) {

                    selectedTech = selectedTech + items[i] + " ";
                    selected[i] = true;
                }
            }

            TextView tv = (TextView) findViewById(R.id.techView);
            tv.setText(selectedTech);
        }
    });

    builder.setMultiChoiceItems(items, selected,
            new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which,
                        boolean isChecked) {
                    selected[which] = isChecked;
                }
            });
    builder.show();
}
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multichoiceitem"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

this is mulchoice item and it contains the checkbox. just run the code if you face any problem ask me. it is a very easier process..




回答2:


You create an like

List<Integer> positionList

and adapter getviewMethod

checbox.setCheckedChanged method in 
positionList.add(position);

and

for(Integer int:positionList){
if(int==position){
yourCheckBox.setChecked(true);

} }




回答3:


I know this question is old but this may be helpful to somebody. If you're extending SimpleCursorAdapter, you will notice that the textviews inside the items of your ListView don't repeat themselves. Only the checkboxes do. Inside your adapter's class, you need:

private ArrayList<Boolean> checkBoxStates = new ArrayList<>();

Inside the constructor, put:

for(int i = 0; i < getCount(); i++) {
    checkBoxStates.add(i, false);
}

and override the getView() method as follows:

public View getView(final int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        CheckBox checkBox = view.findViewById(R.id.checkbox);
        checkBox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(((CheckBox)v).isChecked()){
                    checkBoxStates.set(position, true);
                }else{
                    checkBoxStates.set(position, false);
                }
            }
        });
        checkBox.setChecked(checkBoxStates.get(position));
        return view;
}


来源:https://stackoverflow.com/questions/29208299/checkbox-check-repeats-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!