选中复选框而不选择它

时间:2015-03-07 12:33:12

标签: java android checkbox android-listview

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

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

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

我该怎么办?这是我的代码

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

这是buttonlistener代码:

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

1 个答案:

答案 0 :(得分:1)

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

已经回答here

请参阅此question以了解Listview的回收。

我还建议使用ViewHolder