0

When I click on item its icon is change. But moreover some other icons change. Every 9 or 10 icon is change. Looks like I change icon on every "screen". I don't understand why for 4 days. Please, help.

https://i.stack.imgur.com/oBIew.png

https://i.stack.imgur.com/N8qna.png

Here my ListView in activity_main.xml:

<ListView
    android:id=                     "@+id/lvSimple"
    android:layout_width=           "fill_parent"
    android:layout_height=          "wrap_content"
    android:paddingTop=             "8dp"
    android:paddingBottom=          "8dp"
    android:paddingLeft=            "0dp"
    android:paddingRight=           "0dp"
    android:divider=                "@null"
    android:dividerHeight=          "0dp"
    android:choiceMode="multipleChoice"
    android:descendantFocusability= "blocksDescendants">
</ListView>

list_view_item.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="56dp"
    android:orientation="horizontal"
    android:padding="0dp"
    android:background="@drawable/change_color_on_press"
    android:checkable="true"
    android:id="@+id/item">
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/icon">
        <ImageView
            android:id="@+id/iconFront"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:src="@drawable/ic_folder_white_grey600_36dp">
        </ImageView>
        <ImageView
            android:id="@+id/iconBack"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:alpha="0"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:src="@drawable/ic_check_circle_grey600_white_36dp">
        </ImageView>
    </FrameLayout>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:gravity="start"
        android:paddingTop="16dp"
        android:paddingLeft="0dp"
        android:paddingRight="32dp"
        android:textSize="16sp"
        android:text="TextView">
    </TextView>
</LinearLayout>

MainActivity.java

...
lvSimple = (ListView) findViewById(R.id.lvSimple);
sAdapter = createSimpleAdapter(readFolder(current_folder));
lvSimple.setAdapter(sAdapter);
...
    lvSimple.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,
        View view, int position, long id) {
            ImageView child1_img = (ImageView) view.findViewById(R.id.iconFront);
            ImageView child2_img = (ImageView) view.findViewById(R.id.iconBack);

            final AnimatorSet setLeftIn = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),
                    R.animator.flip_left_in);

            final AnimatorSet setLeftOut = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),
                    R.animator.flip_left_out);

            if(lvSimple.isItemChecked(position)){
                setLeftOut.setTarget(child1_img);
                setLeftIn.setTarget(child2_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, true);
            }
            else{
                setLeftOut.setTarget(child2_img);
                setLeftIn.setTarget(child1_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, false);
            }
        }
    });

...
public String[] readFolder(String path) {
    try {
        if ((new File(path)).isDirectory()) {
            File file = new File(path);
            File[] files = file.listFiles();
            ArrayList<String> file_names = new ArrayList<String>();
            ArrayList<String> dir_names = new ArrayList<String>();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) dir_names.add(files[i].getName());
                else file_names.add(files[i].getName());
            }
            Collections.sort(dir_names);
            Collections.sort(file_names);
            String[] texts = new String[dir_names.size() + file_names.size()];
            int count = 0;
            for (int i = 0; i < dir_names.size(); i++) {
                texts[count] = dir_names.get(i);
                count++;
            }
            for (int i = 0; i < file_names.size(); i++) {
                texts[count] = file_names.get(i);
                count++;
            }
            if (texts.length == 0) {
                list_view_error = true;
                sToast("Directory is empty");
            }
            return texts;
        }
        else {
            sToast("It's file");
            list_view_error = true;
            return (new String[0]);
        }
    } catch (Exception e) {
        sToast("Directory is empty");
        list_view_error = true;
        return (new String[0]);
    }
}
public SimpleAdapter createSimpleAdapter(String[] texts) {
    data = new ArrayList<Map<String, Object>>(texts.length);
    for (int i = 0; i < texts.length; i++) {
        m = new HashMap<String, Object>();
        m.put(ATTRIBUTE_NAME_TEXT, texts[i]);
        if ((new File(current_folder + texts[i])).isDirectory())
            m.put(ATTRIBUTE_NAME_IMAGE, img_folder);
        else m.put(ATTRIBUTE_NAME_IMAGE, img_file);
        data.add(m);
    }
    String[] from = {ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE};
    int[] to = {R.id.text, R.id.iconFront};
    return new SimpleAdapter(this, data, R.layout.list_view_item, from, to);
}
  • Check my answer the post [Single selection in RecyclerView](http://stackoverflow.com/questions/28972049/single-selection-in-recyclerview/29030776#29030776) – Xcihnegn Mar 28 '15 at 09:56

1 Answers1

0

ListView increases the performance of your app by only drawing the rows of your list that are visible on screen. This means that once a row leaves the screen, then it is refreshed, and unless anything stored on it is saved somewhere for the information to be retrieved later, then it is gone for good. Your images changing as you scroll are a result of this.

To counteract this, you can save a reference to the correct image you want to display and retrieve later, which I would do with an ArrayList.

In your OnItemClickListener, instead of having the following:

 if(lvSimple.isItemChecked(position)){
                setLeftOut.setTarget(child1_img);
                setLeftIn.setTarget(child2_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, true);
            }
            else{
                setLeftOut.setTarget(child2_img);
                setLeftIn.setTarget(child1_img);
                setLeftOut.start();
                setLeftIn.start();
                lvSimple.setItemChecked(position, false);

Create a new ArrayList in onCreate:

checkedItems = new ArrayList<>(texts.length); //Assuming texts contains the data you use to populate your ListView

And fill it with what default values you want in a for loop (I assume "unchecked").

Replace your OnItemClickListener code with something like this:

if(checkedItems.get(position).equals("unchecked"){
    //...whatever you want to happen
    checkedItems.set(position, "checked");
    lvSimple.setItemChecked(position, true);
} else {
    //the opposite
}

Then, create a custom ListAdapter and Override the getView() method.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View view;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext().
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row, parent, false);
    } else {
        view = convertView;
    }

    ImageView checkedImage = (ImageView)view.findViewById(//yourImageViewPath);            

    if (checkedItems.get(position).equals("checked"){
        checkedImage.setImageDrawable(//drawable path);
    } else {
        //the opposite
    }

    return view;

Hopefully this is what you're looking for and will resolve your problem.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • it's not what I want, but you give me directoin. so now the problem is out. thanks. –  Mar 30 '15 at 17:43