android Recyclerview 适配器问题 JSON 使用 volley 进行解析

Issue with android Recyclerview adapter in JSON Parsing using volley

我是 android 开发的新手,最近正在开发一个所谓的 covid 跟踪器应用程序。 我 运行 进入 recyclerview 的问题“没有附加适配器跳过布局”。我正在使用 volley 库进行联网并将数据直接解析到我的应用程序中。 json 部分工作得非常好,但我面临的真正问题是我的应用程序在打开后崩溃并且 recyclerview 没有显示任何数据。 我的代码是这样的:

这是主要活动 class:

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerview;
private ExampleAdapter mExampleAdapter;
private ArrayList<exampleItem> mExampleList;
RequestQueue requestQueue;



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


    mRecyclerview = findViewById(R.id.recycler_view);
    mRecyclerview.setHasFixedSize(true);
    mRecyclerview.setLayoutManager(new LinearLayoutManager(this));

    mExampleList = new ArrayList<>();


    requestQueue = Volley.newRequestQueue(this);

    fetchdata();



}
public  void fetchdata(){
    String url = "https://api.rootnet.in/covid19-in/stats/latest";
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new 
Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONObject data = response.getJSONObject("data");
                JSONArray regionalArray = data.getJSONArray("regional");
                for (int i = 0; i < regionalArray.length(); i++) {
                    JSONObject statewise = regionalArray.getJSONObject(i);
                    String statename = statewise.getString("loc");
                    int totalconfirmed = statewise.getInt("totalConfirmed");
                    int discharged = statewise.getInt("discharged");
                    int death = statewise.getInt("deaths");
                    mExampleList.add(new exampleItem(statename,totalconfirmed,discharged,death));
                    Log.i("list",statename+totalconfirmed+discharged+death);

                }
                Log.i("list", mExampleList.toString());
                mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
                mExampleAdapter.notifyDataSetChanged();
                mRecyclerview.setAdapter(mExampleAdapter);



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


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

    requestQueue.add(jsonObjectRequest);
}
}

这是我的适配器class:

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {

private Context mContext;
private ArrayList<exampleItem> mExampleList;

public ExampleAdapter(Context context, ArrayList<exampleItem> exampleList){
    mContext=context;
    mExampleList=exampleList;}

@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.covid_data_layout,parent,false);
    return  new ExampleViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
    exampleItem currentItem = mExampleList.get(position);

    String stateName = currentItem.getState();
    int confirmed = currentItem.getConfirmed();
    int recovered = currentItem.getRecovered();
    int death = currentItem.getDeath();

    holder.statetextView.setText(stateName);
    holder.confirmedtextview.setText(confirmed);
    holder.recoveredTextView.setText(recovered);
    holder.deathtextView.setText(death);
    holder.activeTextView.setText(confirmed-recovered-death);
}

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

public class ExampleViewHolder extends  RecyclerView.ViewHolder{

    public TextView statetextView;
    public TextView confirmedtextview;
    public TextView recoveredTextView;
    public TextView deathtextView;
    public TextView activeTextView;

    public ExampleViewHolder(@NonNull View itemView) {
        super(itemView);
        statetextView = itemView.findViewById(R.id.state_textView);
        confirmedtextview = itemView.findViewById(R.id.confirmed_textView);
        recoveredTextView= itemView.findViewById(R.id.recovered_textView);
        deathtextView = itemView.findViewById(R.id.death_textView);
        activeTextView= itemView.findViewById(R.id.active_textView);

    }
}
}

这是我的 javaclass 供参考:

public class exampleItem {
private String mState;
private int mConfirmed;
private int mRecovered;
private int mDeath;

public  exampleItem(String state, int confirmed,int recovered,int death){
    mState=state;
    mConfirmed=confirmed;
    mRecovered=recovered;
    mDeath=death;
}

public String getState(){
    return mState;
}
public int getConfirmed(){
    return mConfirmed;
}

public int getRecovered(){
    return mRecovered;
}

public  int getDeath(){
    return mDeath;
}
}

如有任何帮助,我将不胜感激。 谢谢!

从您的 MainActivity 中删除 mExampleAdapter.notifyDataSetChanged(); 并在 Adapter 类中像这样更新 onBindViewHolder 的代码。

String stateName = mExampleList.get(position).getState();
int confirmed = mExampleList.get(position).getConfirmed();
int recovered = mExampleList.get(position).getRecovered();
int death = mExampleList.get(position).getDeath();

holder.statetextView.setText(stateName);
holder.confirmedtextview.setText(String.valueOf(confirmed));
holder.recoveredTextView.setText(String.valueOf(recovered));
holder.deathtextView.setText(String.valueOf(death));
holder.activeTextView.setText(String.valueOf(confirmed-recovered-death));