۲ arash parsa
عدم نمایش recyclerview
جامعه جاوا (اندروید) ایجاد شده در ۱۸ اردیبهشت ۱۳۹۹

سلام

من کد ریکوست رو پیاده سازی کردم و الان میخوام لیست دانشجو‌ها رو در recyclerview  نمایش بدم ولی force close  میگیرم و نفهمیدم مشکل از کجاست؟فقط وقتی آداپتر رو اضافه کردم این مشکل پیش اومده و ریکوستم مشکلی نداره 
کسی اگه کمکم کنه ممنون میشم 
کد کلاس main: 
 

package com.sevenlearn.a7learnstudents;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Build;
import android.os.Bundle;
import android.util.EventLogTags;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Method;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import javax.security.auth.login.LoginException;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);
         StringRequest request = new StringRequest(Request.Method.GET, "http://expertdevelopers.ir/api/v1/experts/student",
                 new Response.Listener<String>() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onResponse(String response) {
              //  req.setRetryPolicy(new DefaultRetryPolicy(10000,3,2));
                List<Student> students = new ArrayList<>();
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject studentJsonObject = jsonArray.getJSONObject(i);
                        Student student = new Student();
                        student.setId(studentJsonObject.getInt("id"));
                        student.setScore(studentJsonObject.getInt("score"));
                        student.setCourse(studentJsonObject.getString("course"));
                        student.setFirstName(studentJsonObject.getString("first_name"));
                        student.setLastName(studentJsonObject.getString("last_name"));
                        students.add(student);
                    }
                    Log.i(TAG, "onResponse: " + students.size());
                    RecyclerView recyclerView = findViewById(R.id.rv_student_main);
                    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, RecyclerView.VERTICAL, false));
                    recyclerView.setAdapter(new StudentAdapter(students));
                }
                 catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "onErrorResponse: "+error );
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(request);
    }
}

 

کد کلاس adapter :

 

package com.sevenlearn.a7learnstudents;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.SudentViewHolder> {
    private List<Student> students;
    public StudentAdapter(List<Student> students) {
        this.students = students;
    }
    @NonNull
    @Override
    public SudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SudentViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_student,parent,false));
    }
    @Override
    public void onBindViewHolder(@NonNull SudentViewHolder holder, int position) {
        holder.bind(students.get(position));
    }
    @Override
    public int getItemCount() {
        return students.size();
    }
    public class SudentViewHolder extends RecyclerView.ViewHolder {
        private TextView fullNameTv;
        private TextView courseTv;
        private TextView firstCharTv;
        private TextView scoreTv;
        public SudentViewHolder(@NonNull View itemView) {
            super(itemView);
            fullNameTv = itemView.findViewById(R.id.tv_student_fullName);
            courseTv = itemView.findViewById(R.id.tv_student_course);
            firstCharTv = itemView.findViewById(R.id.tv_student_first_character);
            scoreTv = itemView.findViewById(R.id.tv_student_score);
        }
        public void bind(Student student){
           fullNameTv.setText(student.getFirstName()+" "+student.getLastName());
            courseTv.setText(student.getCourse());
            firstCharTv.setText(student.getFirstName().substring(0,1));
            scoreTv.setText(student.getScore());
        }
    }
}

 

سلام پارسا وقتت بخیر 

کد‌ها رو اگر ممکنه در قالب یک فایل بذارین یا اینکه در قسمتی که میخواین کد‌ها رو کپی کنین فورمت php رو فکر میکنم بهش بدین بهم نمیریزن 

الان قابل خوندن نیستن.

ممنون میشم این کارو کنین.

پوریا شفیعی ۱۸ اردیبهشت ۱۳۹۹، ۱۴:۵۱
MainActivity :
========================================
package com.sevenlearn.a7learnstudents;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Build;
import android.os.Bundle;
import android.util.EventLogTags;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
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.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Method;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import javax.security.auth.login.LoginException;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);
         StringRequest request = new StringRequest(Request.Method.GET, "http://expertdevelopers.ir/api/v1/experts/student",
                 new Response.Listener<String>() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onResponse(String response) {
              //  req.setRetryPolicy(new DefaultRetryPolicy(10000,3,2));
                List<Student> students = new ArrayList<>();
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject studentJsonObject = jsonArray.getJSONObject(i);
                        Student student = new Student();
                        student.setId(studentJsonObject.getInt("id"));
                        student.setScore(studentJsonObject.getInt("score"));
                        student.setCourse(studentJsonObject.getString("course"));
                        student.setFirstName(studentJsonObject.getString("first_name"));
                        student.setLastName(studentJsonObject.getString("last_name"));
                        students.add(student);
                    }
                    Log.i(TAG, "onResponse: " + students.size());
                    RecyclerView recyclerView = findViewById(R.id.rv_student_main);
                    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, RecyclerView.VERTICAL, false));
                    recyclerView.setAdapter(new StudentAdapter(students));
                }
                 catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "onErrorResponse: "+error );
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(request);
    }
}
=============================================================
StudentAdapter: 
package com.sevenlearn.a7learnstudents;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.SudentViewHolder> {
    private List<Student> students;
    public StudentAdapter(List<Student> students) {
        this.students = students;
    }
    @NonNull
    @Override
    public SudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SudentViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_student,parent,false));
    }
    @Override
    public void onBindViewHolder(@NonNull SudentViewHolder holder, int position) {
        holder.bind(students.get(position));
    }
    @Override
    public int getItemCount() {
        return students.size();
    }
    public class SudentViewHolder extends RecyclerView.ViewHolder {
        private TextView fullNameTv;
        private TextView courseTv;
        private TextView firstCharTv;
        private TextView scoreTv;
        public SudentViewHolder(@NonNull View itemView) {
            super(itemView);
            fullNameTv = itemView.findViewById(R.id.tv_student_fullName);
            courseTv = itemView.findViewById(R.id.tv_student_course);
            firstCharTv = itemView.findViewById(R.id.tv_student_first_character);
            scoreTv = itemView.findViewById(R.id.tv_student_score);
        }
        public void bind(Student student){
           fullNameTv.setText(student.getFirstName()+" "+student.getLastName());
            courseTv.setText(student.getCourse());
            firstCharTv.setText(student.getFirstName().substring(0,1));
            scoreTv.setText(student.getScore());
        }
    }
}

 

arash parsa ۱۸ اردیبهشت ۱۳۹۹، ۱۵:۲۴