gpt4 book ai didi

java - RecyclerView 列表不可见

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

我试图通过 recyclerview 从 fragment 上的服务器获取的 json 数据创建一个列表,但它不起作用。一切都很好,但列表没有填充,应用程序没有崩溃,我看不到错误在哪里,请帮我解决这个问题。

示例 json

{
"status": 1,
"message": "success",
"notice": [
{
"id": 21,
"name": "RSP TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583650658.pdf"
},
{
"id": 20,
"name": "RSP TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583650567.pdf"
},
{
"id": 19,
"name": "RSP TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583650554.pdf"
},
{
"id": 18,
"name": "RSP TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583650534.pdf"
},
{
"id": 17,
"name": "RSP TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583650491.pdf"
},
{
"id": 16,
"name": "RSP TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583649707.pdf"
},
{
"id": 15,
"name": "TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583649660.pdf"
},
{
"id": 14,
"name": "TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583649332.pdf"
},
{
"id": 13,
"name": "TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583649185.pdf"
},
{
"id": 12,
"name": "TEST1",
"up_date": "08/03/2020",
"file_address": "noticeFiles/Event_lists1583649031.pdf"
}
]
}

Notifications.java( fragment )

package org.gsac.techfiesta;

import android.app.AlertDialog;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import dmax.dialog.SpotsDialog;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

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.StringRequest;
import com.android.volley.toolbox.Volley;

import org.gsac.techfiesta.Adapters.NoticeAdapter;
import org.gsac.techfiesta.Pojos.NoticePojo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class Notifications extends Fragment {
AlertDialog dialog;
RecyclerView mList;
LinearLayoutManager manager;
List<NoticePojo> noticePojoList;
RecyclerView.Adapter adapter;

public Notifications() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_notifications, container, false);

mList = v.findViewById(R.id.rv_notice);
noticePojoList = new ArrayList<>();
adapter = new NoticeAdapter(getContext(), noticePojoList);
manager = new LinearLayoutManager(getContext());
manager.setOrientation(LinearLayoutManager.VERTICAL);


mList.setHasFixedSize(true);
mList.setLayoutManager(manager);
mList.setAdapter(adapter);

//getNotices();

return v;
}

private void getNotices() {
String url = "https://example/api/v1/getNotices.php";
dialog = new SpotsDialog.Builder().setContext(getContext()).build();
dialog.setMessage("Loading...");
dialog.show();
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
int status = jsonObject.getInt("status");
if (status == 1) {
JSONArray jsonArray = jsonObject.getJSONArray("notice");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject noticeObject = jsonArray.getJSONObject(i);
NoticePojo noticePojo = new NoticePojo();
noticePojo.setId(noticeObject.getInt("id"));
Log.i("NOTICE", "" + noticeObject.getInt("id"));
noticePojo.setName(noticeObject.getString("name"));
noticePojo.setDate(noticeObject.getString("up_date"));
noticePojo.setAddress(noticeObject.getString("file_address"));
//Line of code added from here ---------------
noticePojoList.add(noticePojo);
// to here -----------------------------------
}
Log.i("NOTICE", "State Changed!");
adapter.notifyDataSetChanged();
dialog.dismiss();
} else {
dialog.dismiss();
Toast.makeText(getContext(), "Failed to get Notices", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
dialog.dismiss();
Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getContext(), "Network issue", Toast.LENGTH_LONG).show();
}
});

requestQueue.add(stringRequest);

}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
getNotices();
} else {
// Do your Work
}
}

}

fragment_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Notifications">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_notice"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

lv_single_notice.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="@color/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/iv_single_notice"
android:src="@drawable/ic_notifications_black_24dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"/>
<TextView
android:layout_toRightOf="@id/iv_single_notice"
android:text="TESTING"
android:id="@+id/tv_notice_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#fff"/>
<TextView
android:layout_below="@id/tv_notice_name"
android:layout_marginLeft="52dp"
android:text="2020"
android:id="@+id/tv_notice_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#fff"/>
<ImageView
android:layout_marginTop="15dp"
android:layout_alignParentEnd="true"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_keyboard_arrow_right_black_24dp"/>
<View
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#C8C3C3"/>
</RelativeLayout>
</LinearLayout>

NoticePojo.java

package org.gsac.techfiesta.Pojos;

public class NoticePojo {
public String name,address,date;
public int id;

public NoticePojo(int id, String name, String address, String date){
this.id=id;
this.name=name;
this.address=address;
this.date=date;
}

public NoticePojo(){

}

public int getId(){
return this.id;
}
public String getName(){
return this.name;
}
public String getAddress(){
return this.address;
}
public String getDate(){
return this.date;
}

public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setAddress(String address){
this.address=address;
}
public void setDate(String date){
this.date=date;
}

}

NoticeAdapter.java

package org.gsac.techfiesta.Adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.gsac.techfiesta.Pojos.NoticePojo;
import org.gsac.techfiesta.R;

import java.util.List;

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

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.ViewHolder> {

private Context context;
private List<NoticePojo> list;

public NoticeAdapter(Context context, List<NoticePojo> list){
this.context=context;
this.list=list;
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position){
NoticePojo noticePojo = list.get(position);
holder.noticeName.setText(noticePojo.getName());
holder.noticeDate.setText(noticePojo.getAddress());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView noticeName,noticeDate;
public ViewHolder(@NonNull View itemView) {
super(itemView);
noticeName=itemView.findViewById(R.id.tv_notice_name);
noticeDate=itemView.findViewById(R.id.tv_notice_date);
}
}
}

最佳答案

您错过了将 noticePojo 添加到 noticePojoList

在 for 循环中添加以下行。

noticePojoList.add(noticePojo);

关于java - RecyclerView 列表不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60594845/

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