0

In my listview I have a view with checkbox and sounds. When I select one checkbox others is selected too and I don´t know why.

In this video I show the problem:

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

What can I do? This is my code

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>

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

And this is the buttonlistener code:

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);
    }
brainovergrow
  • 458
  • 4
  • 13
tatyana
  • 55
  • 7

1 Answers1

1

In your video it's seen that selection is occurring at proper intervals. So the problem should be, your convertView getting reused.

It is already answered here.

See this question to understand Listview's recycling.

I would also recommend using a ViewHolder.

Community
  • 1
  • 1
Sajith Sageer
  • 163
  • 3
  • 16