我希望使用上下文在视图适配器类中获取共享首选项,但它返回null。
我尝试过的是:
如何在getitemviewtype中获取适配器的登录参数?
我编辑了以下代码
任何帮助都会很好
import static android.preference.PreferenceManager.getDefaultSharedPreferences;
public class ChattAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Chatt> moviesList;
Context context;
public String loginx;
public TextView title, year, genre;
public CardView cardViewphoto;
public ImageView resim;
private static final int TYPE_ITEM0 = 0;
private static final int TYPE_ITEM1 = 1;
SharedPreferences pref;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView titlem, yearm, genrem;
public CardView cardViewphoto;
public ImageView resim;
public MyViewHolder(View view) {
super(view);
titlem = (TextView) view.findViewById(R.id.title);
genrem = (TextView) view.findViewById(R.id.genre);
yearm = (TextView) view.findViewById(R.id.year);
resim= (ImageView) view.findViewById(R.id.profile_image);
}
}
class ViewHolder0 extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public CardView cardViewphoto;
public ImageView resim;
public ViewHolder0(View v) {
super(v);
title = (TextView) v.findViewById(R.id.titlesender);
genre = (TextView) v.findViewById(R.id.genresender);
year = (TextView) v.findViewById(R.id.yearsender);
// resim= (ImageView) v.findViewById(R.id.profile_image);
} }
public ChattAdapter(List<Chatt> moviesList) {
this.moviesList = moviesList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
pref =PreferenceManager.getDefaultSharedPreferences(context);
//SharedPreferences sharedPreferences = context.getSharedPreferences("FileName", Context.MODE_PRIVATE);
loginx = pref.getString("user", null);
Log.d("prefsss",loginx.toString());
// login= FirebaseAuth.getInstance().getCurrentUser().toString();
if (viewType == 1 ){
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list_row, parent, false));
}
else{
return new ViewHolder0(LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list_row2, parent, false));
}
// View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list_row, parent, false);
//return new MyViewHolder(itemView);
}
@Override
public int getItemViewType(int position ) {
// Here you have to write logic for which position have which type of row and return the same
Chatt love = moviesList.get(position);
Log.d("law",love.getTitle().toString());
Log.d("prefsss",loginx.toString());
//Log.d("law",login);
Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;
// pref =PreferenceManager.getDefaultSharedPreferences(context);
//SharedPreferences sharedPreferences = context.getSharedPreferences("FileName", Context.MODE_PRIVATE);
// login = pref.getString("user", null);
if( love.getTitle().equals(loginx)){
return 2;
}else{
return 1;
}
}
@Override
public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
Log.d("lawtayp",String.valueOf(holder.getItemViewType()) );
if (holder.getItemViewType()==2){
ViewHolder0 viewHolder0 = (ViewHolder0)holder;
Chatt love = moviesList.get(position);
// viewHolder0.title.setText(love.getTitle());
// viewHolder0.genre.setText(love.getGenre());
viewHolder0.year.setText(love.getYear());
// Picasso.with(getClass()).load("http://barronpw.alwaysdata.net/p/"+name+".png").into(cardViewphoto);
// Picasso.with(context).load("http://barronpw.alwaysdata.net/p/"+love.getTitle() +".png").into(viewHolder0.resim);
}else{
// code to populate type 1 view here
MyViewHolder myviewholder = (MyViewHolder)holder;
Chatt love = moviesList.get(position);
// Chatt love = moviesList.get(position);
// myviewholder.titlem.setText(love.getTitle());
// myviewholder.genrem.setText(love.getGenre());
myviewholder.yearm.setText(love.getYear());
// Picasso.with(getClass()).load("http://barronpw.alwaysdata.net/p/"+name+".png").into(cardViewphoto);
Picasso.with(context).load("http://barronpw.alwaysdata.net/p/"+love.getTitle() +"-p.png").into(myviewholder.resim);
}
// Code to populate type 0 view here
}
@Override
public int getItemCount() {
return moviesList.size();
}
答案 0 :(得分:0)
试试这个
public class ChattAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Chatt> moviesList;
Context context;
public String loginx;
public TextView title, year, genre;
public CardView cardViewphoto;
public ImageView resim;
private static final int TYPE_ITEM0 = 0;
private static final int TYPE_ITEM1 = 1;
SharedPreferences pref;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView titlem, yearm, genrem;
public CardView cardViewphoto;
public ImageView resim;
public ChattAdapter(Context context){
this.context = context;
}
....
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// context = parent.getContext();
pref =PreferenceManager.getDefaultSharedPreferences(context);
....
}
答案 1 :(得分:0)
问题是应该在应用程序中的上下文,以便可以正确读取共享首选项。一种方法是使用构造函数将activity / fragment的上下文作为附加参数传递给适配器。
答案 2 :(得分:0)
制作构造函数:
public ChattAdpater(Context context){
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
从onCreateViewHolder中删除prefs初始化