图层不会与recycleview一起显示

时间:2018-04-21 18:18:03

标签: android json xml recyclerview-layout

我正在做一个比特币跟踪器应用程序,所以我可以学习json解析的基础知识,但我的应用程序层在运行应用程序时保持空白。这是我的 RecycleViewAdapter:

package Adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.view.menu.MenuView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.angel.simplecrypto.R;

import java.util.List;

import Models.Currencies;

public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.MyViewHolder> {

    private Context mContext;
    private List<Currencies> mData;
    RequestOptions option;

    public RecyclerViewAdapter(Context mContext, List<Currencies> mData) {
        this.mContext = mContext;
        this.mData = mData;
        option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);


    }

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

        View view;
        LayoutInflater inflater=LayoutInflater.from(mContext);
        view = inflater.inflate(R.layout.currency_row_item, parent,false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        holder.currency_price.setText(mData.get(position).getEur());
        holder.currency_price2.setText(mData.get(position).getUsd());



    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder{


        TextView currency_price2;
        TextView currency_price;


        public MyViewHolder(View itemView) {
            super(itemView);


            currency_price2 = itemView.findViewById(R.id.price2);
            currency_price= itemView.findViewById(R.id.price);



        }
    }
}

这是我的MainActivity:

package Activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.angel.simplecrypto.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import Adapters.RecyclerViewAdapter;
import Models.Currencies;

public class MainActivity extends AppCompatActivity {

    private final String JSON_URL = "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=EUR,USD";
    private JsonArrayRequest request;
    private RequestQueue requestQueue;
    private List<Currencies> lstCurrencies;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstCurrencies = new ArrayList<>();
        recyclerView = findViewById(R.id.recyclerviewid);
        jsonrequest();
    }

    private void jsonrequest() {
        request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                JSONObject jsonObject = null;
                for (int i = 0; i < response.length(); i++){

                    try{
                        jsonObject=response.getJSONObject(i);
                        Currencies currencies = new Currencies();
                        currencies.setEur(jsonObject.getString("EUR"));
                        currencies.setUsd(jsonObject.getString("USD"));


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            setuprecyclerview(lstCurrencies);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });



    }

    private void setuprecyclerview(List<Currencies> lstCurrencies) {

        RecyclerViewAdapter myadapter = new RecyclerViewAdapter(this, lstCurrencies);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(myadapter);
    }

}

我只是想用2个项目测试回收查看器,但我不知道为什么没有出现。在我的 activity_main.xml 中,我创建了recyclerviewer,并且我有一个单独的.xml 来设计该recycleviewer中的项目。有任何想法吗?顺便说一下,我得到了这个错误:04-21 18:20:11.707 26586-26586 / com.example.angel.simplecrypto E / RecyclerView:没有连接适配器;跳过布局

3 个答案:

答案 0 :(得分:1)

我想你忘记了lstCurrencies.add(currencies);

答案 1 :(得分:0)

您需要在jsonrequest()方法

中添加此内容

lstCurrencies.add(currencies);

并定义您的RecyclerViewAdapter适配器类全局,并在listview中添加数据后添加myadapter.notifyDataSetChanged();

尝试这样......

public class MainActivity extends AppCompatActivity {

    private final String JSON_URL = "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=EUR,USD";
    private JsonArrayRequest request;
    private RequestQueue requestQueue;
    private List<Currencies> lstCurrencies;
    private RecyclerView recyclerView;
    RecyclerViewAdapter myadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstCurrencies = new ArrayList<>();
        recyclerView = findViewById(R.id.recyclerviewid);

        myadapter = new RecyclerViewAdapter(this, lstCurrencies);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(myadapter); 

        jsonrequest();
    }

    private void jsonrequest() {
        request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                JSONObject jsonObject = null;
                for (int i = 0; i < response.length(); i++){

                    try{
                        jsonObject=response.getJSONObject(i);
                        Currencies currencies = new Currencies();
                        currencies.setEur(jsonObject.getString("EUR"));
                        currencies.setUsd(jsonObject.getString("USD"));
                        lstCurrencies.add(currencies);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

     RequestQueue queue = Volley.newRequestQueue(this);
     queue.add(request);

    } 

}

答案 2 :(得分:0)

您的json解析不正确,因此数据不会显示在recyclerview.Your API中返回json对象,您将解析array.Check您的API响应并检查您的json解析编码。