你好,我有一个水平的recyclerview,其中包含一些产品,在加号按钮,我添加产品到购物车,产品添加后的图标变化。我做到了这一点,但我的问题是保存这个按钮的状态,因为当我浏览fragmetsactivities之间,当应用程序启动按钮保持不变。下面是我的情况。
我的适配器里的代码是:
@Override
public void onBindViewHolder(@NonNull ArtikujtViewHolder holder, int position) {
final Artikujt mArtikull = artikujt.get(position);
String mArtikullName = mArtikull.getEmertimet();
holder.artikullName.setText(mArtikullName);
String mArtikullCmimi = mArtikull.getCmimi().toString();
holder.artikullCmimi.setText(mArtikullCmimi+" Leke");
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(mContext, ProductDetailsActivity.class);
intent.putExtra("positon", artikujt.get(position));
intent.putExtra("mArtikullName", mArtikullName);
intent.putExtra("mArtikullCmimi", mArtikullCmimi);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
});
SharedPreferences preferences = mContext
.getSharedPreferences(ITEMS_PREF, MODE_PRIVATE);
SharedPreferences.Editor mEditor = preferences.edit();
// using shared preferences to save the state
if (preferences.contains(ITEMS_PREF)) {
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.ic_check_black, 0);
holder.addItems.setEnabled(false);
} else {
}
holder.addItems.setOnClickListener(v -> {
Gson gson = new Gson();
String json = preferences.getString("artikujtShporta", "");
ArrayList<Artikujt> artikullObject = gson
.fromJson(json, new TypeToken<ArrayList<Artikujt>>(){}.getType());
if (artikullObject != null) {
artikullObject.add(mArtikull);
String jsonString = gson.toJson(artikullObject);
mEditor.putString("artikujtShporta", jsonString);
mEditor.apply();
} else {
ArrayList<Artikujt> arrayArtikuj = new ArrayList<>();
arrayArtikuj.add(mArtikull);
Type listOfTestObject = new TypeToken<ArrayList<Artikujt>>() {}.getType();
String s = gson.toJson(arrayArtikuj, listOfTestObject);
mEditor.putString("artikujtShporta", s);
mEditor.apply();
}
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.ic_check_black, 0);
//======================================================================================
// Display a toast message
final Toast toast = Toast.makeText(mContext, R.string.shtuar_ne_shporte, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
Handler handler = new Handler();
handler.postDelayed(toast::cancel, 1000);
//======================================================================================
assert artikullObject != null;
count_key = artikullObject.size();
String cartCount = String.valueOf(count_key);
Intent my_intent = new Intent("msg");
my_intent.putExtra("cart_count", cartCount);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(my_intent);
v.setEnabled(false);
});
}
我找了很多关于这个问题的资料 解决办法可能是用sharedprerences.contains(preferences_id)来保存状态 但是我很困惑. 有没有人有什么建议,我怎么能实现这一点或可能的解决方案.Thanks in advance.
我假设你想保持的是 button
因为它是点击后。
如果这是你的问题,是的,我们将使用。shared prefrences
:
@Override
public void onBindViewHolder(@NonNull ArtikujtViewHolder holder, int position) {
//the item
final Artikujt mArtikull = artikujt.get(position);
..........
..........
//read the prefrences
SharedPreferences pref = getSharedPreferences("Button", MODE_PRIVATE);
String state = pref.getString(String.valueOf(position)+"pressed", "no");
if(state.equals("yes")){
//the button must be pressed, make it pressed (check mark icon)
//change the icon
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_check_black, 0);
holder.addItems.setEnabled(false);
}else{
//the button must not be pressed, make it not pressed (default add icon)
//change the icon
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_add_icon, 0);
holder.addItems.setEnabled(true);
}
//after clicking the add icon
holder.addItems.setOnClickListener(v -> {
//save to preferences
SharedPreferences.Editor editor = getSharedPreferences("Button", MODE_PRIVATE).edit();
editor.putString(String.valueOf(position)+ "pressed", "yes");
editor.apply();
//change the icon
holder.addItems.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_check_black, 0);
//======================================================================================
// Display a toast message
final Toast toast = Toast.makeText(mContext, R.string.shtuar_ne_shporte, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
Handler handler = new Handler();
handler.postDelayed(toast::cancel, 1000);
//======================================================================================
assert artikullObject != null;
count_key = artikullObject.size();
String cartCount = String.valueOf(count_key);
Intent my_intent = new Intent("msg");
my_intent.putExtra("cart_count", cartCount);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(my_intent);
v.setEnabled(false);
});
}
在大多数情况下,保存模型列表中按钮的状态就足够了。但是,当用户改变它的片段或活动时,你的模型列表有可能因为内存的使用而被系统破坏。在这种情况下,你必须选择使用sharedpref,file,db等方式来持久化你的数据,还是将你的数据保存在一个比活动或片段寿命更长的存储中。似乎你分享的页面与你的应用程序的主要功能有关,所以你应该将你的数据保存在一个存储库中,其范围取决于应用程序的范围。我建议你看看mvvm架构和存储库模式。