如何将JSONArray从sharedpreferences转换为Adapter

时间:2018-04-30 20:55:13

标签: android json android-sharedpreferences

在我的应用中加载的第一个活动中,我将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();

我可以在另一个string中获取此Activity,然后轻松将其转换为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);
        }

但是,当我将上述代码放入我onBindViewHolder的{​​{1}}或adapter中的任何其他位置时,我得到:adapter并且我的应用崩溃了。

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

它崩溃的地方说:

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

1 个答案:

答案 0 :(得分:3)

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

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