gpt4 book ai didi

android - 将 SharedPreferences 添加到 Recycler 适配器中的项目

转载 作者:行者123 更新时间:2023-11-30 01:58:19 26 4
gpt4 key购买 nike

如果 RecyclerView 中的项目被选中但我不知道如何操作,我希望能够 checkin 其他 Activity。我尝试使用 SharedPreferences,但只得到一个空对象引用。

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at com.icedex.lolmon.testapp.firststart.FirstStartRecyclerAdapter$ViewHolder.<init>(FirstStartRecyclerAdapter.java:63)
at com.icedex.lolmon.testapp.firststart.FirstStartRecyclerAdapter.onCreateViewHolder(FirstStartRecyclerAdapter.java:35)
at com.icedex.lolmon.testapp.firststart.FirstStartRecyclerAdapter.onCreateViewHolder(FirstStartRecyclerAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5116)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4416)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:437)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2713)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1626)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是我的代码:

public class FirstStartRecyclerAdapter extends RecyclerView.Adapter<FirstStartRecyclerAdapter.ViewHolder> {

public static final String TAG = "FSRecyclerAdapter";
public ArrayList<Card> cards;
public Context context;

public SharedPreferences sharedPref;

public boolean checkMusic = false;

public FirstStartRecyclerAdapter(Context context, ArrayList<Card> cards) {
this.cards = cards;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);

return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(FirstStartRecyclerAdapter.ViewHolder holder, int position) {

Card card = cards.get(position);
holder.card_tv.setText(card.cardName);
holder.card_iv.setImageResource(card.getDrawable());
holder.card_iv2.setImageResource(card.getDrawable2());
}

public int getItemCount() {
return cards.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public ImageView card_iv;
public ImageView card_iv2;
public TextView card_tv;

public ViewHolder(View itemView) {
super(itemView);
card_tv = (TextView) itemView.findViewById(R.id.card_tv);
card_iv = (ImageView) itemView.findViewById(R.id.card_iv);
card_iv2 = (ImageView) itemView.findViewById(R.id.card_iv2);

itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
int position = getLayoutPosition();
Log.d(TAG, "clicked position:" + position);
long postId = cards.get(position).getId();

sharedPref = context.getSharedPreferences("myPref", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPref.edit();

edit.putBoolean("checkWear", false);
edit.apply();

if (postId == 1) {

if (sharedPref.getBoolean("checkWear", true)) {
card_iv2.setImageResource(R.drawable.checkbox_blank_circle_outline);
edit.putBoolean("checkWear", false);
edit.apply();
} else {
card_iv2.setImageResource(R.drawable.checkbox_marked_circle);
edit.putBoolean("checkWear", true);
edit.apply();
}
}

if (postId == 2) {

if (checkMusic) {
card_iv2.setImageResource(R.drawable.checkbox_blank_circle_outline);
checkMusic = false;
} else {
card_iv2.setImageResource(R.drawable.checkbox_marked_circle);
checkMusic = true;
}
}
}
}
}

如有任何帮助,我将不胜感激。

最佳答案

Nullpointer 对我来说看起来很奇怪......无论如何这是另一种方法:

创建一个保存状态的静态类

final class State{  
static boolean somethingIsChecked = false;

static boolean isSomethingChecked(){
return somethingIsChecked;
}

static void setSomethingIsChecked(boolean isChecked){
somethingIsChecked = isChecked;
}
}

然后调用您的 ViewHolders onClick

State.setSomethingIsChecked(somevalue);

其他 Activity 现在可以调用

State.isSomethingClicked();

获取值(value)


请注意,无论是 Sharedpreferences 还是静态类都不能处理指向同一个变量的多个实例,因此您应该在您的 App 结构中确保它们始终是 singleInstance。

关于android - 将 SharedPreferences 添加到 Recycler 适配器中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31851049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com