如何从 sharedpreferences 获取 JSONArray 到 Adapter

how to get JSONArray from sharedpreferences into Adapter

在我的应用程序中加载的第一个 activity 中,我将我的 JSONArray 放入 Sharedpreferences(作为 String):

    //put myJsonArray into shared preferences file as a String
    //Convert back to Json later, in the adapter
    SharedPreferences sharedPrefs = getSharedPreferences("MyData", Context.MODE_PRIVATE);
    //we want to edit SharedPreferences
    SharedPreferences.Editor editor = sharedPrefs.edit();
    //put the string value into SharedPreferences, with the key "key_value"
    editor.putString("key_value", myJsonArray.toString());
    //commit the string
    editor.commit();

我可以在另一个 Activity 中得到这个 string 并使用以下方法轻松将其转换回 JSONArray

SharedPreferences sharedPrefs = getSharedPreferences("MyData", Context.MODE_PRIVATE);
        String json_array = sharedPrefs.getString("key_value", "0");
        try
        {
            JSONArray jsonArray = new JSONArray(json_array);

        } catch (JSONException e) {
            Log.e("MYAPP", "unexpected JSON exception", e);
        }

但是,当我将上述代码放入 adapteronBindViewHolderadapter 的其他任何位置时,我得到:NullPointerException 和我的应用程序崩溃。

请告诉我如何解决这个问题。

它崩溃的地方说:

 java.lang.NullPointerException at com.example.chris.tutorialspoint.SharedReviews.SharedPopulistoReviewsAdapter.onBindViewHolder(SharedPopulistoReviewsAdapter.java:130)

我的onBindViewHolder代码是:

 @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {


        SharedReview r = the_Shared_reviews.get(position);

        SharedPreferences sharedPrefs = context.getSharedPreferences("MyData", Context.MODE_PRIVATE);
        String json_array = sharedPrefs.getString("key_value", "0");
        try
        {
            JSONArray jsonArray = new JSONArray(json_array);
            System.out.println("SharedAdapter, the jsonarray is :" + jsonArray);


        } catch (JSONException e) {
            Log.e("MYAPP", "unexpected JSON exception", e);
        }

        ((ReviewHolder) viewHolder).category.setText("Category: " + r.getCategory());
        ((ReviewHolder) viewHolder).name.setText("Name: " + r.getName());
        ((ReviewHolder) viewHolder).phone.setText("Phone: " + r.getPhone());
        ((ReviewHolder) viewHolder).comment.setText("Your Comment: " + r.getComment());

        //set an onClick listener for the row, if it's clicked anywhere
        ((ReviewHolder) viewHolder).itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            //When the review is clicked in PopulistoListView
            //then show that review
            public void onClick(View v) {

                SharedReview sharedReview = (SharedReview) SharedPopulistoReviewsAdapter.getItem(position);

                //we want to pass the review_id of the sharedReview being clicked
                //to the ViewContact activity, and from there post it and get more
                //info for that sharedReview - address, comments etc
                Intent i = new Intent(v.getContext(), ViewContact.class);
                //pass the review_id to ViewContact class
                i.putExtra("review_id", sharedReview.getReviewid());
                v.getContext().startActivity(i);
            }

        });
    }

Line 130 是:

SharedPreferences sharedPrefs = context.getSharedPreferences("MyData", Context.MODE_PRIVATE);

发生这种情况是因为您尚未初始化 context。您必须在创建适配器对象时通过将上下文作为参数传递来初始化 context,或者您可以从视图中获取上下文。 要从视图中获取上下文,请将行更改为:

SharedPreferences sharedPrefs = ((ReviewHolder) viewHolder).category.getContext().getSharedPreferences("MyData", Context.MODE_PRIVATE);