如何保存项目的按钮src状态

时间:2019-01-03 15:18:51

标签: java android kotlin

我要为商品创建收藏夹列表 我正在使用SharedPreference将项目保存到FavoriteList

public void addFavorite(Context context, TilekItem item) {
    List<TilekItem> favorites = getFavorites(context);
    if (favorites == null)
        favorites = new ArrayList<>();
    favorites.add(item);
    saveFavorites(context, favorites);
}
public ArrayList<TilekItem> getFavorites(Context context) {
    SharedPreferences settings;
    List<TilekItem> favorites;

    settings = context.getSharedPreferences(PREFS_NAME,
            Context.MODE_PRIVATE);

    if (settings.contains(FAVORITES)) {
        String jsonFavorites = settings.getString(FAVORITES, null);
        Gson gson = new Gson();
        TilekItem[] favoriteItems = gson.fromJson(jsonFavorites,
                TilekItem[].class);

        favorites = Arrays.asList(favoriteItems);
        favorites = new ArrayList(favorites);
    } else
        return null;

    return (ArrayList<TilekItem>) favorites;
}

有两种添加到收藏夹列表并从列表中获取所有项目的方法

要保存到DetailActivity中的收藏夹列表中,我使用FloatingActionButton

DetailActivity.class

class DetailActivity : AppCompatActivity() {

var isFavorite = false
var textForFavorite = ""
var sharedPreference: SharedPreference? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_detail)
    val tilek = getIntent().getStringExtra("TILEK")
    val type = getIntent().getStringExtra("TYPE")
    val id = getIntent().getIntExtra("ID", 0)

    tilek_detail.text = tilek
    sharedPreference = SharedPreference()

    val item = TilekItem(id, tilek, type)
    isFavorite = checkFavoriteItem(item)

    fab.setOnClickListener { view ->
        if (!isFavorite) {
            isFavorite = true
            textForFavorite = "Added to"
            fab.setImageDrawable(ContextCompat.getDrawable(this@DetailActivity, R.drawable.ic_star_yellow))

            sharedPreference!!.addFavorite(this@DetailActivity, item)
        } else {
            isFavorite = false
            textForFavorite = "Removed from"
            sharedPreference!!.removeFavorite(this@DetailActivity, item)
            fab.setImageDrawable(ContextCompat.getDrawable(this@DetailActivity, R.drawable.ic_star))

        }
        Snackbar.make(view, "$textForFavorite favourite list", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show()

    }
    initToolbar()

}

fun checkFavoriteItem(checkItem: TilekItem): Boolean {
    val favorites = sharedPreference?.getFavorites(this@DetailActivity)
    val result = favorites!!.contains(checkItem)
    return result
}

使用此代码,我可以保存到收藏夹列表 但是当我fab.setImageDrawable(ContextCompat.getDrawable(this@DetailActivity, R.drawable.ic_star_yellow)不保存时 重新打开项目 如何保存此状态?

1 个答案:

答案 0 :(得分:1)

看起来已保存但未显示。您忘了点击之前setImageDrawable(...)

if (!isFavorite) {
  fab.setImageDrawable(ContextCompat.getDrawable(this@DetailActivity, R.drawable.ic_star_yellow))
} else {
  fab.setImageDrawable(ContextCompat.getDrawable(this@DetailActivity, R.drawable.ic_star))
}