我有recyclerView,我喜欢5个cardView,每个都有切换按钮,所以当我点击它们时,它们完美地说(打开,关闭),但问题是当我重新启动应用程序这个切换按钮我决定要开始关闭我尝试使用sharedpereference但它没有用,所以这里是我的代码可以有人帮助和谢谢
public class MyAdapter extends RecyclerView.Adapter<MyHolder>{
Context c;
String[] players;
String[] positions;
int[] images;
public MyAdapter(Context ctx,String[] players,String[] positions,int[] images)
{
this.c=ctx;
this.players=players;
this.positions=positions;
this.images=images;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder holder=new MyHolder(v);
SharedPreferences sharedPrefs = c.getSharedPreferences("com.example.xyz", MODE_PRIVATE);
holder.fav.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.nameTxt.setText(players[position]);
holder.posTxt.setText(positions[position]);
holder.img.setImageResource(images[position]);
// holder.fav.setChecked(false);
// holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (holder.fav.isChecked())
{
SharedPreferences.Editor editor = c.getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.commit();
}
else
{
SharedPreferences.Editor editor = c.getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", false);
editor.commit();
}
if (isChecked)
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light));
else
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
}
});
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
Intent i=new Intent(c,DetailActivity.class);
i.putExtra("Name",players[position]);
i.putExtra("Position",positions[position]);
i.putExtra("Image",images[position]);
c.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return players.length;
}
答案 0 :(得分:0)
使用此共享首选项 对于设置
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(Activity);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("KEY, true/false);
editor.commit();
获取
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(context);
boolean check=prefs.getBoolean(fieldName, false);
答案 1 :(得分:0)
您可以共享您的共享首选项实现,以便我们可以更正吗?
此代码仅显示如何向UI显示数据但不保存检查状态。
您需要以某种方式保存检查状态并在应用启动时加载它们。几乎没有简单的解决方案。 因此,您需要创建一个对象列表,其中每个对象包含相关数据,包括检查状态,并将该列表保存在共享首选项或任何其他数据存储中。 或者,如果您的按钮数量不变,您可以单独为每个按钮状态保存。 (chk1 = true,chk2 = false等...) 但我不推荐这种解决方案,因为它不适应物品数量的动态改变。
答案 2 :(得分:0)
您使用"com.example.xyle"
阅读首选项,使用"com.example.xyz"
保存首选项。你必须在两种情况下都使用相同的字符串。
<强>更新强>
您应该从holder.fav.setChecked(false);
移除onBindViewHolder
,因为您在onCreateViewHolder
上设置了[Tue Aug 29 06:25:02.680766 2017] [ssl:warn] [pid 8282:tid
140418378721152] AH01909: mydomainname.com:443:0 server certificate
does NOT include an ID which matches the server name
[Tue Aug 29 06:25:02.681138 2017] [wsgi:warn] [pid 8282:tid
140418378721152] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Aug 29 06:25:02.681148 2017] [wsgi:warn] [pid 8282:tid
140418378721152] mod_wsgi: Runtime using Python/3.5.2.
[Tue Aug 29 06:25:02.683899 2017] [mpm_event:notice] [pid 8282:tid
140418378721152] AH00489: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g m
od_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Tue Aug 29 06:25:02.683936 2017] [core:notice] [pid 8282:tid
140418378721152] AH00094: Command line: '/usr/sbin/apache2'
[Wed Aug 30 06:25:01.905011 2017] [mpm_event:notice] [pid 8282:tid
140418378721152] AH00493: SIGUSR1 received. Doing graceful restart
AH00558: apache2: Could not reliably determine the server's fully
qualified domain name, using fe80::705e:5bff:fe93:1faf. Set the
'ServerName' directive globally to suppress this message
。