0

I have a list view with implement on click element

    mProfiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            fileName=profileListViewAdapter.getItem(position).getRawName();
            if(!selectionMode) {
                    profileParser.parseProfile(getApplicationContext(), fileName, true);
                profileSelected();
            }
            else {
                if(!selectedProfiles.contains(new SelectedProfilesDataModel(fileName,position))) {
                    selectedProfiles.add(new SelectedProfilesDataModel(fileName,position));
                    ImageView imageView=(ImageView)view.findViewById(R.id.imageViewHouseProfile);
                    imageView.setImageResource(R.drawable.picked);
                }
                else {
                    selectedProfiles.remove(new SelectedProfilesDataModel(fileName,position));
                    ImageView imageView=(ImageView)view.findViewById(R.id.imageViewHouseProfile);
                    imageView.setImageResource(R.mipmap.house_icon);
                }

            }
        }
    });

After the element has been tapped I am adding to the list and changing the icon on the list view.

I can choose multiple elements at one time. Everything is working is working perfectly until I delete some of the elements.

Code of the method

@OnClick(R.id.buttonDeleteProfile)
public void deleteProfile(){
    selectionMode =false;
    for(SelectedProfilesDataModel spdm:selectedProfiles) {
        File file = new File(getFilesDir(), spdm.getName());
        profileParser.deleteProfile(file);

    }
        profileListViewAdapter.clear();
        fileProfiles = profileParser.listProfilesFiles(getApplicationContext());
        profileListViewAdapter.setData(fileProfiles);
        profileListViewAdapter.notifyDataSetChanged();
}

The method is removing the element from the list view, but the problem is that removed element (let say index two) has element under index three then element there will be element two after deletion. Element number two had another icon set as element three, but after deletion old element three is getting "selected icon" of old element two.

This is how it looks in practice.

Before deleting

enter image description here

After deleting

enter image description here

Does anyone have an idea how I can reset the image views of all other elements after deletion of one of them?

Yoni
  • 1,346
  • 3
  • 16
  • 38
GosiaK
  • 105
  • 1
  • 10

1 Answers1

0

I found a solution thanks to this question

How can I update a single row in a ListView?

After deleting I set manually the image on positions which have an index of previously deleted rows.

public void deleteProfile(){
    selectionMode =false;
    for(SelectedProfilesDataModel spdm:selectedProfiles) {
        File file = new File(getFilesDir(), spdm.getName());
        profileParser.deleteProfile(file);

    }
        profileListViewAdapter.clear();
        fileProfiles = profileParser.listProfilesFiles(getApplicationContext());
        profileListViewAdapter.setData(fileProfiles);
    for(SelectedProfilesDataModel spdm:selectedProfiles) {
        updateListView(spdm.getPosition());
    }
        profileListViewAdapter.notifyDataSetChanged();
}

This is my delete function

public void updateListView(int index){
    View view = mProfiles.getChildAt(index -
            mProfiles.getFirstVisiblePosition());
    if(view == null)
        return;
    ImageView imageView=(ImageView)view.findViewById(R.id.imageViewHouseProfile);
    imageView.setImageResource(R.mipmap.house_icon);

}

and function for changing the icon.

GosiaK
  • 105
  • 1
  • 10