gpt4 book ai didi

java - 为什么我的 Recyclerview 在 Activity 中不起作用

转载 作者:行者123 更新时间:2023-12-02 08:42:25 25 4
gpt4 key购买 nike

我需要解析 json 并在 RecyclerView 上显示它们..但是我的 RecyclerView 不起作用..我需要解决这个问题..

这是 Json

我的应用程序的简单 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"
}
]

这是学生信息 Activity

我认为在这个 Activity 中没有什么问题

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 解析完成时,您应该在 addItemsFromJSON() 方法中调用 adapter.notifyDataSetChanged();

关于java - 为什么我的 Recyclerview 在 Activity 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61299357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com