2017-09-01 70 views
0

我有掣子元件的Android - 修改列表視圖元素刪除另一個元素

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

      } 
     } 
    }); 

上實現的列表視圖元素被竊聽了我添加到列表和列表視圖改變圖標後後。

我可以一次選擇多個元素。 一切工作正常,直到我刪除了一些元素。

方法

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

的方法除去從列表視圖的元件,但問題是,除去的元件(比方說指數爲兩個)的碼具有下指數三元然後元件會有元件兩個刪除後。元素二將另一個圖標設置爲元素三,但刪除後舊元素三正在獲取舊元素二的「選定圖標」。

這是它在實踐中的樣子。

之前刪除

enter image description here

刪除

enter image description here

有沒有人有一個想法,我可以怎麼他們中的一個刪除後重新設置所有其他元素的形象意見後?

回答

0

我找到了解決由於這個問題

How can I update a single row in a ListView?

刪除我手動設置在圖像上具有先前被刪除的行的索引位置之後。

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

這是我的刪除功能

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

} 

和功能改變的圖標。

相關問題