从片段到非活动类获取字符串,然后转换为JSONObject

时间:2016-12-02 03:04:24

标签: java android android-fragments sharedpreferences

我正在尝试将I从片段传递给非活动类,并将字符串转换为JSONObject。

在我的片段中,我将所需的字符串存储在共享首选项

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.store_list, container, false);

    //super.onCreate();
    VolleyGet.getInstance(getActivity());
    VolleyGet.getInstance().VolleyGET(domain, api, new CustomListener<String>(){
        @Override
        public void getResult(String result){
            if (!result.isEmpty())
            {
                Log.e("storelist---------->", result);

                SharedPreferences sp1=getActivity().getSharedPreferences("storeList", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp1.edit();
                editor.putString("storeList", result);
                editor.commit();
            }
        }
    });

在我的非活动课程中,我尝试获得这样的共享偏好:

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


Context context;
JSONArray storeArray;



SharedPreferences preferences = context.getSharedPreferences("storeList", Activity.MODE_PRIVATE);
String storeList = preferences.getString("storeList", "");


System.out.println("storelist", storeList);
storeArray = new JSONArray(storeList);
JSONObject jObj = storeArray.getJSONObject(0);
List<Movie>     mItems;

    public CardAdapter() {
        super();
        mItems = new ArrayList<Movie>();
        Movie movie = new Movie();
        movie.setName("this will come from the JSONObject");
        movie.setThumbnail(R.drawable.will come from JSONObject);
        mItems.add(movie);

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.store_cardview, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        Movie movie = mItems.get(i);
        viewHolder.tvMovie.setText(movie.getName());
        viewHolder.imgThumbnail.setImageResource(movie.getThumbnail());
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView imgThumbnail;
        public TextView tvMovie;

        public ViewHolder(View itemView) {
            super(itemView);
            imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
            tvMovie = (TextView)itemView.findViewById(R.id.tv_movie);
        }
    }

}

我收到错误1.无法解析符号println 2.Unknown类:storelist 3.storearray也未知

请帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

首先,你不能if (_$dataTable) { var deferred = $.Deferred(), json = null; $.getJSON('http://localhost:3000/members/list/01tuee743h9muf6', function (data) { deferred.resolve(data); }).fail(function (error) { deferred.reject(error); }); $.when(deferred).done(function (json) { json = Utils.dataCleanup(json); _$dataTable.bootstrapTable({ striped: true, iconSize: 'btn-sm', cache: false, search: true, searchOnEnterKey: true, searchText: 'Search for a member...', data: json, columns: [ { checkbox: true, field: 'check', clickToSelect: true, searchable: false }, { field: 'email', title: 'Name', sortable: true, clickToSelect: false }, { field: 'role', title: 'Type', sortable: true, clickToSelect: false }, { field: 'actions', title: 'Actions', sortable: false, clickToSelect: false, formatter: function () { return '<img class="control" src="/assets/images/delete.svg" alt="Delete">'; }, events: function () { // TODO: add some events... } } ] }); }).fail(function (error) { console.error(error); }); return deferred.promise(); } 或访问你已经完成它的未初始化的上下文(在你的问题范围之外)。

关于其他错误,我认为你没有显示正确的代码,但如果错误显示“storelist”,那么你的变量实际上是System.out.println,所以这看起来像一个错字。

查看RecyclerView适配器的任何教程,构造函数通常提供Context。这就是访问SharedPreferences所需的内容。

storeList