我需要解析json并将它们显示在RecyclerView上。但是我的RecyclerView无法正常工作。
我需要解决这个问题。

这是杰森

适用于我的应用程序的简单Json文件

[
  {
    "name": "Saif Maroof",
    "roll": 978617,
    "gender":"Male",
    "phonenumber": 01931078639,
    "StudentImage": "R.drawble.books"
    },

    {
    "name": "Minhaj",
    "roll": 978617,
    "gender":"Male",
    "phonenumber": 01931078639,
    "studentimage": "R.drawble.books"
    }
]



这是StudentInfo活动

我认为这次活动没有错

package com.college.npc17_18.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;


import com.college.npc17_18.MainActivity;
import com.college.npc17_18.R;
import com.college.npc17_18.adapter.StudentInfoAdapter;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class StudentInfo extends AppCompatActivity {

    RecyclerView recyclerView;

    StudentInfoAdapter studentInfoAdapter;

    RecyclerView.Adapter adapter;

   // ProgressBar progressBar;

   RecyclerView.LayoutManager layoutManager;

    List <Object> studentInfo = new ArrayList<>();

    DividerItemDecoration dividerItemDecoration;

    private static final String TAG = "MainActivity";

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

       getSupportActionBar().setDisplayHomeAsUpEnabled(true);

     //  progressBar = findViewById(R.id.progress_circular_country);

       recyclerView = findViewById(R.id.studentsInfoRecycleView);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);


        recyclerView.setLayoutManager(layoutManager);

       adapter = new StudentInfoAdapter(this,studentInfo);

//        String jsonFileString = Utils.getJsonFromAssets(getApplicationContext(), "studentinfo.json");
//        progressBar.setVisibility(View.GONE);

//        studentInfoAdapter = new StudentInfoAdapter(this,studentInfo);
        recyclerView.setAdapter(adapter);




        addItemsFromJSON();


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }



    private void addItemsFromJSON() {
        try {

            String jsonDataString = readJSONDataFromFile();
            JSONArray jsonArray = new JSONArray(jsonDataString);

            for (int i=0; i<jsonArray.length(); ++i) {

                JSONObject itemObj = jsonArray.getJSONObject(i);

                String name = itemObj.getString("name");
                String roll = itemObj.getString ("roll");
                String phonenumber = itemObj.getString("phonenumber");
                String gender = itemObj.getString("gender");
                int studentimage = itemObj.getInt("studentimage");

               com.college.npc17_18.model.StudentInfo studentInfos = new com.college.npc17_18.model.StudentInfo(name,roll,phonenumber,gender,studentimage);

                studentInfo.add(studentInfos);
            }

        } catch (JSONException | IOException e) {
            Log.d(TAG, "addItemsFromJSON: ", e);
        }
    }

    private String readJSONDataFromFile() throws IOException{

        InputStream inputStream = null;
        StringBuilder builder = new StringBuilder();

        try {

            String jsonString = null;
            inputStream = getResources().openRawResource(R.raw.studentinfo);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream, "UTF-8"));

            while ((jsonString = bufferedReader.readLine()) != null) {
                builder.append(jsonString);
            }

        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return new String(builder);
    }
}



这是StudentInfo适配器

package com.college.npc17_18.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.college.npc17_18.R;
import com.college.npc17_18.model.StudentInfo;

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

public class StudentInfoAdapter extends RecyclerView.Adapter <StudentInfoAdapter.Viewholder> {
    private List <Object> studentInfos;
    Context context;

    public StudentInfoAdapter(Context context, List<Object> studentInfo) {
        this.studentInfos = studentInfo;
        this.context = context;
    }

    @NonNull
    @Override
    public StudentInfoAdapter.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.studentitem,parent,false);
        return new Viewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StudentInfoAdapter.Viewholder holder, int position) {
        StudentInfo studentInfo = (StudentInfo) studentInfos.get(position);
        holder.StudentImageId.setImageResource (studentInfo.getStudentImage());
        holder.NameId.setText("Name:"+studentInfo.getNamme());
        holder.RollId.setText("Roll"+studentInfo.getRoll());
        holder.GenderId.setText("Gender:"+studentInfo.getGender());
        holder.PhoneNumberId.setText("Phone Number:"+studentInfo.getPhoneNumber());

    }

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


    public class Viewholder extends  RecyclerView.ViewHolder{
        TextView NameId,RollId,PhoneNumberId,GenderId;
        ImageView StudentImageId;

        public Viewholder(@NonNull View itemView) {
            super(itemView);


            StudentImageId = itemView.findViewById(R.id.StudentImageId);
            NameId = itemView.findViewById(R.id.NameId);
            RollId = itemView.findViewById(R.id.RollId);
           // CGPAId = itemView.findViewById(R.id.CGPAId);
            GenderId = itemView.findViewById(R.id.GenderId);
            PhoneNumberId = itemView.findViewById(R.id.PhoneNumberId);
        }
    }
}



这是StudentItem.xml

RecyclerView的布局...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">

   <androidx.cardview.widget.CardView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="10dp"
       android:clickable="true"
       android:elevation="40dp"
       app:cardCornerRadius="5dp">

       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_margin="10dp"
           android:orientation="horizontal">

           <ImageView
               android:id="@+id/StudentImageId"
               android:layout_width="71dp"
               android:layout_height="match_parent" />


           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_marginLeft="10dp"
               android:orientation="vertical">

               <TextView
                   android:id="@+id/NameId"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="Name"
                   android:textSize="18sp" />

               <TextView
                   android:id="@+id/RollId"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="Roll" />

               <TextView
                   android:id="@+id/GenderId"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="Gender" />

               <TextView
                   android:id="@+id/PhoneNumberId"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="Phone Number" />


           </LinearLayout>
       </LinearLayout>
   </androidx.cardview.widget.CardView>
</LinearLayout>


现在我该怎么办?

最佳答案

在实例化适配器类的对象时

adapter = new StudentInfoAdapter(this,studentInfo);

您的adapter对象用空的studentInfo列表初始化

因此,当JSON解析完成时,您应该在adapter.notifyDataSetChanged();方法中调用addItemsFromJSON()

08-06 05:37