0

我有一個列表視圖,在我的主片段中每行都有一個書籤圖標的鈴聲。並且我還有另一個片段來顯示它內部的優化鈴聲(當我點擊主碎片內的書籤圖標時,該特定的行數據將被保存到共享偏好設置中並添加到最喜歡的片段中)。從共享首選項檢索數據後,ListView不會刷新

現在我的問題是,當我點擊一個項目,使其成爲收藏它不會立即顯示在我最喜歡的片段。我嘗試了所有這些:

listView.requestLayout(); 
listView.refreshDrawableState(); 
listView.invalidate(); 
listView.invalidateViews(); 
adapter.notifyDataSetChanged(); 

沒有一個可行。或者我只需分離並附上最喜歡的Fragment或關閉應用程序並重新打開它。

你能幫助我嗎?

這是我的項目添加到我的共享偏好內主要碎片

@Override 
public boolean favOnClick(int position , View v) { 
    ProductLight product = soundList.get(position); 
    ImageView button = (ImageView) v.findViewById(R.id.favImageHive); 
    String tag = button.getTag().toString(); 
    if (tag.equalsIgnoreCase("grey")) { 
     sharedPreference.addFavorite(product); 
     snackS("Added to Favorites"); 
     button.setTag("red"); 
     button.setImageResource(R.mipmap.bookmarked); 
    } else { 
     sharedPreference.removeFavorite(product); 
     button.setTag("grey"); 
     button.setImageResource(R.mipmap.bookmark_border); 
     snackS("Removed from Favorites"); 
    } 
    return true; 
} 

,我讓我的最愛列表從共享偏好最喜歡的片段這樣的:

private List<ProductLight> soundList = new ArrayList<ProductLight>(); 
. 
. 
. 

soundList = sharedPreference.getFavorites(); 
adapter = new CustomLightAdapter(activity, soundList); 
listView.setAdapter(adapter); 

共享首選項

public class SharedPreference_light { 

public static final String PREFS_NAME = "Light_Products"; 
public static final String FAVORITES = "Favorite_Tones_Light"; 

SharedPreferences settings; 
SharedPreferences.Editor editor; 
Gson gson = new Gson(); 

public SharedPreference_light(Context context) { 
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
    editor = settings.edit(); 
} 

// This four methods are used for maintaining favorites. 
public void saveFavorites(List<ProductLight> favorites) { 
    String jsonFavorites = gson.toJson(favorites); 
    editor.putString(FAVORITES, jsonFavorites); 
    editor.apply(); 
} 

public void addFavorite(ProductLight product) { 
    List <ProductLight> favorites = getFavorites(); 
    if (favorites == null) 
     favorites = new ArrayList<ProductLight>(); 
    favorites.add(product); 
    saveFavorites(favorites); 
    Log.w("addPrefLog", favorites.toString()); 
} 

public void removeFavorite(ProductLight product) { 
    ArrayList <ProductLight> favorites = getFavorites(); 
    if (favorites != null) { 
     favorites.remove(product); 
     saveFavorites(favorites); 
    } 
} 

public ArrayList <ProductLight> getFavorites() { 
    List<ProductLight> favorites; 
    if (settings.contains(FAVORITES)) { 
     String jsonFavorites = settings.getString(FAVORITES, null); 
     ProductLight[] favoriteItems = gson.fromJson(jsonFavorites, ProductLight[].class); 
     favorites = Arrays.asList(favoriteItems); 
     favorites = new ArrayList <ProductLight> (favorites); 
    } else 
     return null; 

    return (ArrayList <ProductLight>) favorites; 
    } 
} 
+0

從列表片段調用最喜歡的片段onclick fav按鈕,它將更新fav項目列表 –

+0

editor.apply()是一個異步函數,也許你可以調用onSharedPreferenceChanged中的接口監聽器。或者使用editor.commit()來代替? – faruk

+0

@FarukItsDO是的,但如果我保存太多的項目,commit()會減慢我的listview滾動。我試了一下,它不工作。 – DastakWall

回答

1

當您將數據保存到共享首選項時,您希望列表被更新。這可以通過使用共享首選項更改偵聽器來完成。 在主Fragment中實現SharedPreferences.OnSharedPreferenceChangeListener並重寫onSharedPreferenceChanged()方法。確保在oncreate()方法中註冊共享首選項更改偵聽器,即 ,即sharedPreferences.registerOnSharedPreferenceChangeListener(this); 並在onDestroy()中取消註冊。

因此,當您將數據添加到共享首選項時,您將在onSharedPreferenceChanged()中獲得回調。因此,在此方法中添加您的邏輯來刷新您的列表。

+0

我認爲我的共享首選項工作正常。因爲(考慮我在Favorite Fragment中有一個項目,並在該列表視圖中顯示)現在在我的主要片段中,如果單擊以取消標記該項目,收藏夾片段中的書籤圖標將變爲未選中,但項目本身仍然存在。 – DastakWall

0

您分配一個列表的ListView適配器

adapter = new CustomLightAdapter(activity, soundList) //soundList should be a global variable;

當改變的soundList應該被更新。然後調用adapter.notifyDataSetChanged()將使該更改反映在ListView中