Rohit Mahto
Rohit Mahto

Reputation: 256

RecyclerView List Not Visible

I was trying to make a list through recyclerview from json data fetched from server on a fragment but its not working. Everything is fine, but the list is not populating, the app isnt crashing, I cant see where the bug is, please help me solve this.

example 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 (Fragement)

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);
        }
    }
}

Upvotes: 0

Views: 75

Answers (2)

Muhammad Yaseen
Muhammad Yaseen

Reputation: 278

Hello i also have the same issue there is a simple solution do not need to worry about it because you need to set Layout Manager to your recyclerview.

Actually A layout manager is an object that implements the Layout Manager interface* and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container.

So just add the below code before setting adapter with your recyclerview.

rvFoodTaken.setLayoutManager(new LinearLayoutManager(MainActivity.this));

I hop that will solve your problem

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33258

You are missed to add noticePojo into noticePojoList

Add below line inside your for loop.

noticePojoList.add(noticePojo);

Upvotes: 2

Related Questions