java - 复选框已选中但未选择它

标签 java android checkbox android-listview

在我的 ListView 中,我有一个带有复选框和声音的 View 。当我选择一个复选框时,其他复选框也会被选中,我不知道为什么。

在此视频中我展示了问题:

http://www.dailymotion.com/video/x2iu5mn_aplication_tech

我能做什么?这是我的代码

自定义 View .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="match_parent"
    android:orientation="horizontal" >

    <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox1"

    />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/text1"/>

    <ImageView
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/imageView"
    android:src="@android:drawable/ic_menu_add"
    />
    </LinearLayout>

自定义ArrayAdapter.java

private class myArrayAdapter2 extends ArrayAdapter<String>{

 private HashMap<Integer, Boolean> myChecked = new HashMap<Integer,Boolean>();

    public myArrayAdapter2(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);

        for(int i = 0; i < objects.size(); i++){
            myChecked.put(i, false);
        }
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;


        if(row==null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.custom_view, parent, false);
        }

        TextView textview = (TextView)row.findViewById(R.id.text1);
        textview.setText(myList.get(position));
        CheckBox checkBox=(CheckBox) row.findViewById(R.id.checkBox1);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    ruta.add(
                        Environment.getExternalStorageDirectory().getPath() 
                        + "/test/" + myList.get(position)
                        );
                 } else {
                    shared = ruta.indexOf(
                        Environment.getExternalStorageDirectory().getPath() 
                        + "/test/" + myList.get(position)
                        );
                    ruta.remove(shared);
                }
            }
        });
        return row;
    }
}

这是按钮监听器代码:

public void AudioClick(View view) {
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/test");
        List<String> myList = new ArrayList<String>();
        File list[] = file.listFiles();

        for( int i=0; i< list.length; i++){
            myList.add( list[i].getName() );
        }

        ArrayAdapter myArrayAdapter = new MyArrayAdapter(
            this,
            R.layout.custom_textview,
            android.R.id.text1,
            myList
        );

        listvi.setAdapter(myArrayAdapter);
    }

最佳答案

在您的视频中可以看到,选择是以适当的间隔进行的。所以问题应该是,您的 convertView 被重用。

已经回答here .

查看此question了解Listview的回收。

我还建议使用 ViewHolder

关于java - 复选框已选中但未选择它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28914915/

相关文章:

java - 包罗万象的 SMTP 服务器 + GUI

Java/XML : use a string in another string

android - 为什么添加 abiFilters 可使应用程序正常工作?

javascript - 如何使用 jquery 根据复选框状态获取隐藏字段的值

javascript - 避免重复的 JavaScript

javascript - Angularjs - 禁用和取消选中复选框

java - EyeGesture和EyeGestureManager需要清晰

java - BluetoothAdapter StartDiscovery 在前台服务中不起作用

java - 什么是NullPointerException,我该如何解决?

java - Android EditText : java. lang.IndexOutOfBoundsException : getChars (0 . .. 16) 超出长度 15