如何在android studio中使用retrofit和recylerview渲染渲染JSON数据

时间:2017-06-26 16:01:19

标签: android android-recyclerview retrofit2

我正在尝试使用改造和recyclerview从Web应用程序显示。我有以下型号:

主要活动

public class ProfileActivity extends AppCompatActivity {
    private SalesAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private SaleService mService;

    @Override
    protected void onCreate (Bundle savedInstanceState)  {
        super.onCreate( savedInstanceState );
        setContentView(R.layout.activity_profile );
        mService = ApiUtils.getSaleService();
        mRecyclerView = (RecyclerView) findViewById(R.id.rvItems);
        mAdapter = new SalesAdapter(this, new ArrayList<Sale>(0), new SalesAdapter.ItemListener() {
            @Override
            public void onPostClick(long id) {

            }
        });


        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true);
        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);

        loadAnswers();
    }

    public void loadAnswers() {
        mService.getSales().enqueue(new Callback<List<Sale>>() {
            @Override
            public void onResponse(Call<List<Sale>> call, Response<List<Sale>> response) {

                if(response.isSuccessful()) {
                    mAdapter.UpdateSales(response.body());
                } else {
                    int statuscode = response.code();
                }
            }

            @Override
            public void onFailure(Call<List<Sale>> call, Throwable t) {

            }
        });
    }
}

Recyclerview适配器

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

    private Context mContext;
    private List<Sale> mSales;
    private  ItemListener mItemListener;

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView tvLocaleName;
        ItemListener mItemListener;

        public ViewHolder(View itemView, ItemListener itemListener) {
            super(itemView);

            tvLocaleName = (TextView) itemView.findViewById(android.R.id.text1);
            this.mItemListener = itemListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Sale sale = getSales(getAdapterPosition());
            this.mItemListener.onPostClick(sale.getId());
            notifyDataSetChanged();
        }
    }

    public SalesAdapter(Context mContext, List<Sale> mSales, ItemListener mItemListener) {
        this.mContext = mContext;
        this.mSales = mSales;
        this.mItemListener = mItemListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ViewHolder(view, this.mItemListener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Sale sale = mSales.get(position);
        TextView textView = holder.tvLocaleName;
        textView.setText(sale.getLocaleName());
    }

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



    public void UpdateSales(List<Sale> sales) {
        mSales = sales;
    }

    private Sale getSales(int position) {
        return mSales.get(position);
    }

    public interface ItemListener {
        void onPostClick(long id);
    }
}

enter image description here

从图像中你可以看到我能够获取数据,但是如何在其中显示recyclerview?我在哪里遇到代码错误?

1 个答案:

答案 0 :(得分:0)

刷新列表后,向适配器添加notifyDataSetChanged()调用。

您的方法应如下所示:

public void UpdateSales(List<Sale> sales) {
    mSales = sales;
    notifyDataSetChanged();
}

查看this link以了解有关此方法的更多信息。