我想在点击时保存cardview的标题。我很确定我正在以正确的方式发送文本标题但由于某种原因,我无法在单击某个项目时保存文本。顺便说一下,每当点击该项目时,它会打开另一个名为“收藏夹”的活动,它会显示我想要的文本,但每当我手动打开它时,它都不会显示任何内容。请参考此图像作为参考。
任何帮助都会很高兴。感谢。
点击
之前点击
这是我的RecyclerAdapter。我尝试以两种方式发送数据,intent和SharedPreferences但两者都不起作用
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder>{
Context context;
ArrayList<Album> albums;
public MyRecyclerAdapter(Context c, ArrayList<Album> albums){
this.context=c;
this.albums=albums;
}
// Initialize holder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
//Bind data to views
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTxt.setText(albums.get(position).getName());
holder.img.setImageResource(albums.get(position).getImage());
//listener
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View v, int pos) {
Toast.makeText(context,albums.get(pos).getName() + " ,added to favorite ",Toast.LENGTH_SHORT).show();
// sending data using intent
Intent intent = new Intent(context, favorite.class);
intent.putExtra("text", albums.get(pos).getName());
intent.putExtra("t", albums.get(pos).getImage());
context.startActivity(intent);
// data sharing using sharedPref
SharedPreferences.Editor editor = context.getApplicationContext().getSharedPreferences("lado", MODE_PRIVATE).edit();
editor.putString("mug",albums.get(pos).getName());
editor.apply();
}
});
}
@Override
public int getItemCount() {
return albums.size();
}
}
这是我的另一项活动,我想保存并显示文字。
public class favorite extends AppCompatActivity {
@BindView(R.id.demoText)
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
ButterKnife.bind(this);
/**
* I'm using bundle here
*/
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
String value2 = bundle.getString("text");
tv.setText(value2);
}
/**
* I'm using sharedPref here
*/
SharedPreferences prefs = getSharedPreferences("lado", MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("mug", "No name defined");//"No name defined" is the default value.
tv.setText(name);
}
}
}
答案 0 :(得分:0)
您首先启动收藏的活动,然后将数据保存在SharedPreferences
中。
尝试先在SharedPreferences
中保存值,然后启动活动并尝试在下一个活动中获取值,您将获得。
编辑:
holder.setItemClickListener(new ItemClickListener(){ @覆盖 public void onItemClick(View v,int pos){
Toast.makeText(context,albums.get(pos).getName() + " ,added to favorite ",Toast.LENGTH_SHORT).show();
// data sharing using sharedPref
SharedPreferences.Editor editor = context.getApplicationContext().getSharedPreferences("lado", MODE_PRIVATE).edit();
editor.putString("mug",albums.get(pos).getName());
editor.apply();
// sending data using intent
Intent intent = new Intent(context, favorite.class);
intent.putExtra("text", albums.get(pos).getName());
intent.putExtra("t", albums.get(pos).getImage());
context.startActivity(intent);
}
});
在收藏的活动中,您应该"mug"
而不是"text"
,因为您没有设置&#34; text&#34;在第一次活动
SharedPreferences prefs = getSharedPreferences("lado", MODE_PRIVATE);
//String restoredText = prefs.getString("text", null);
//if (restoredText != null) {
String name = prefs.getString("mug", "No name defined");//"No name defined" is the default value.
tv.setText(name);
// }