如何使用Json

时间:2016-07-04 11:53:08

标签: java android arrays json android-recyclerview

我有一个带有5个项目的RecyclerView并且拖放了掉落功能。我想将项目的位置保存到SharedPreferences中,以便在启动App时恢复它们。目前我可以使用Json将其位置保存到SharedPreferences中,但是当我恢复位置时应用程序崩溃了。有解决方案吗感谢。

    public static AppCompatActivity mActivity;
    public static SharedPreferences SP;
    public static SharedPreferences.Editor mEditor;
    public static RecyclerView mRecyclerView;
    public static RecyclerViewDragDropManager mRecyclerViewDragDropManager;
    public static List<Long> mSaveItemsOrder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mActivity = this;

        SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        mEditor = SP.edit();

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerViewDragDropManager = new RecyclerViewDragDropManager();
        mRecyclerViewDragDropManager.setInitiateOnMove(false);
        mRecyclerViewDragDropManager.setInitiateOnLongPress(true);
        mRecyclerViewDragDropManager.setLongPressTimeout(300);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mRecyclerViewDragDropManager.createWrappedAdapter(new Adapter()));
        mRecyclerViewDragDropManager.attachRecyclerView(mRecyclerView);
    }

    public static class Item {
        public final long id;
        public final String text;

        public Item(long id, String text) {
            this.id = id;
            this.text = text;
        }
    }

    public static class ViewHolder extends AbstractDraggableItemViewHolder {

        Button mButton;

        public ViewHolder(View itemView) {
            super(itemView);
            mButton = (Button) itemView.findViewById(R.id.button);
        }

    }

    public static class Adapter extends RecyclerView.Adapter<ViewHolder> implements DraggableItemAdapter<ViewHolder> {

        List<Item> mItems;

        public Adapter() {
            setHasStableIds(true);

            mItems = new ArrayList<>();
            if (SP.getString("items_order", null) == null) {
                for (int mInt = 0; mInt < 5; mInt++) {
                    mItems.add(new Item(mInt, "Item " + mInt));
                }
            } else {
                mItems = new Gson().fromJson(SP.getString("items_order", null), new TypeToken<Item>(){}.getType());
            }

        }

        @Override
        public long getItemId(int position) {
            return mItems.get(position).id;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view, parent, false);
            return new ViewHolder(mView);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            Item mItem = mItems.get(position);
            holder.mButton.setText(mItem.text);
        }

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

        @Override
        public void onMoveItem(int fromPosition, int toPosition) {
            Item mMovedItem = mItems.remove(fromPosition);
            mItems.add(toPosition, mMovedItem);
            notifyItemMoved(fromPosition, toPosition);
        }

        @Override
        public boolean onCheckCanStartDrag(ViewHolder holder, int position, int x, int y) {
            return true;
        }

        @Override
        public ItemDraggableRange onGetItemDraggableRange(ViewHolder holder, int position) {
            return null;
        }

        @Override
        public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
            return true;
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        mSaveItemsOrder = new ArrayList<>();
        for (int mInt = 0; mInt < mRecyclerView.getAdapter().getItemCount(); mInt++) {
            mSaveItemsOrder.add(mRecyclerView.getAdapter().getItemId(mInt));
        }

        mEditor.putString("items_order", new Gson().toJson(mSaveItemsOrder)).commit();
    }

这是错误: https://www.dropbox.com/s/j2tf4n08depyynt/Error.txt?dl=0

1 个答案:

答案 0 :(得分:0)

问题在于你的JSON。 您要序列化为JSON的对象是mSaveItemsOrder,它是ArrayList。但是在反序列化时,你给GSON类new TypeToken<Item>(){}.getType()。 所以你将一个ArrayList存储在你的JSON中,但试图反序列化一个不同的类,这是行不通的 这应该有效:

mItems = new Gson().fromJson(SP.getString("items_order", null),  new TypeToken<ArrayList>(){}.getType(););