0

I am trying to make an application where using Recycle View and Volley to get data from server and also I used Navigation drawer and fragments,Everything working fine except When no data on recycle-view I want to show a notice like "there is no data!"I searched over internet multiple times but haven't get a proper solution or I couldn't understand properly because I am totally beginner.

bellow is my java files

1.Adapter

package com.eworld.myapplication;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;

import java.util.List;

public class ExchangesAdapter extends RecyclerView.Adapter<ExchangesViewHolder> {

    private List<ExchangesSetterGetter>exchangeList;
    private Context context;

    public ExchangesAdapter(List<ExchangesSetterGetter> exchangeList, Context context) {
        this.exchangeList = exchangeList;
        this.context = context;
    }

    @NonNull
    @Override
    public ExchangesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardlayout,viewGroup,false);
        ExchangesViewHolder viewHolder=new ExchangesViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ExchangesViewHolder exchangesViewHolder, int i) {
        String status="";


        final ExchangesSetterGetter exchangesPosition=exchangeList.get(i);

            if (exchangesPosition.getStatus().equals("1")) {
                status = "Awaiting Confirmation";
            } else if (exchangesPosition.getStatus().equals("2")) {
                status = "Awaiting Payment";
            } else if (exchangesPosition.getStatus().equals("3")) {
                status = "Processing";
            } else if (exchangesPosition.getStatus().equals("4")) {
                status = "proceed";
            } else if (exchangesPosition.getStatus().equals("5")) {
                status = "Timeout";
            } else if (exchangesPosition.getStatus().equals("6")) {
                status = "Denied";
            } else if (exchangesPosition.getStatus().equals("7")) {
                status = "Canceled";
            } else if (exchangesPosition.getStatus().equals("8")) {
                status = "Unknown";
            }
            exchangesViewHolder.gatewayFrom.setText(exchangesPosition.getAmountFrom() + " " + exchangesPosition.getCurrencyFrom() + " " + exchangesPosition.getExchangeFrom());
            exchangesViewHolder.gatewayTo.setText(exchangesPosition.getAmountTo() + " " + exchangesPosition.getCurrencyTo() + " " + exchangesPosition.getExchangeTo());
            exchangesViewHolder.status.setText(status);

            Picasso.get().load("https://eworld.ltd/" + exchangesPosition.getImgSend()).into(exchangesViewHolder.imgSendFrom);
            Picasso.get().load("https://eworld.ltd/" + exchangesPosition.getImgReceived()).into(exchangesViewHolder.imgSendTo);




    }

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

2.View Holder

    package com.eworld.myapplication;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ExchangesViewHolder extends RecyclerView.ViewHolder {

    ImageView imgSendFrom,imgSendTo;
    TextView gatewayFrom,gatewayTo,status;
    public ExchangesViewHolder(@NonNull View itemView) {
        super(itemView);

        imgSendFrom=itemView.findViewById(R.id.img1);
        imgSendTo=itemView.findViewById(R.id.img2);
        gatewayFrom=itemView.findViewById(R.id.tv1);
        gatewayTo=itemView.findViewById(R.id.tv2);
        status=itemView.findViewById(R.id.tv3);
    }
}

3.Getter and Setter(Data model)

package com.eworld.myapplication;

public class ExchangesSetterGetter {

    private String exchangeFrom,exchangeTo,status,imgSend,imgReceived,currencyFrom,currencyTo,amountFrom,amountTo;


    public ExchangesSetterGetter(String exchangeFrom, String exchangeTo, String status, String imgSend, String imgReceived, String currencyFrom, String currencyTo, String amountFrom, String amountTo) {
        this.exchangeFrom = exchangeFrom;
        this.exchangeTo = exchangeTo;
        this.status = status;
        this.imgSend = imgSend;
        this.imgReceived = imgReceived;
        this.currencyFrom = currencyFrom;
        this.currencyTo = currencyTo;
        this.amountFrom = amountFrom;
        this.amountTo = amountTo;

    }

    public String getCurrencyFrom() {
        return currencyFrom;
    }

    public void setCurrencyFrom(String currencyFrom) {
        this.currencyFrom = currencyFrom;
    }

    public String getCurrencyTo() {
        return currencyTo;
    }

    public void setCurrencyTo(String currencyTo) {
        this.currencyTo = currencyTo;
    }

    public String getAmountFrom() {
        return amountFrom;
    }

    public void setAmountFrom(String amountFrom) {
        this.amountFrom = amountFrom;
    }

    public String getAmountTo() {
        return amountTo;
    }

    public void setAmountTo(String amountTo) {
        this.amountTo = amountTo;
    }

    public String getExchangeFrom() {
        return exchangeFrom;
    }

    public void setExchangeFrom(String exchangeFrom) {
        this.exchangeFrom = exchangeFrom;
    }

    public String getExchangeTo() {
        return exchangeTo;
    }

    public void setExchangeTo(String exchangeTo) {
        this.exchangeTo = exchangeTo;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getImgSend() {
        return imgSend;
    }

    public void setImgSend(String imgSend) {
        this.imgSend = imgSend;
    }

    public String getImgReceived() {
        return imgReceived;
    }

    public void setImgReceived(String imgReceived) {
        this.imgReceived = imgReceived;
    }

}

4.And finally my fragment Activity with recycle-view

package com.eworld.myapplication;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class ExchangesFrag extends Fragment {
    RecyclerView recyclerView;
    ExchangesAdapter adapter;
    List<ExchangesSetterGetter> listItems;
    SharedPrefManager sharedPreferences;
    int uid;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View exchanges=inflater.inflate(R.layout.exchanges_layout,container,false);
        recyclerView=exchanges.findViewById(R.id.rview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listItems=new ArrayList<>();
        sharedPreferences=new SharedPrefManager(getActivity());
        uid = sharedPreferences.getUser().getId();
        loadData();
        return exchanges;
    }
    public void loadData() {
        StringRequest stringRequest=new StringRequest(Request.Method.GET, URLs.url+uid, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject receive = jsonArray.getJSONObject(i);
                            ExchangesSetterGetter exchangesSetterGetter = new ExchangesSetterGetter(
                                    receive.getString("exchangeFrom"),
                                    receive.getString("exchangeTo"),
                                    receive.getString("status"),
                                    receive.getString("imgSend"),
                                    receive.getString("imgReceive"),
                                    receive.getString("sendCurrency"),
                                    receive.getString("receiveCurrency"),
                                    receive.getString("amount_send"),
                                    receive.getString("amount_receive")
                            );
                            listItems.add(exchangesSetterGetter);
                        }
                        adapter = new ExchangesAdapter(listItems, getContext());
                        recyclerView.setAdapter(adapter);
                    } catch(JSONException e){
                        e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(),"error on volley",Toast.LENGTH_LONG).show();
            }
        });
        RequestQueue queue= Volley.newRequestQueue(getContext());
        queue.add(stringRequest);
    }
}
  • 1
    A simple way is to check in your network request if there is any data present or not. If not you can hide your recycler view and show a text view with your desired message – p.mathew13 May 21 '19 at 11:55
  • [its easy and i was check out some time ago you will check out](https://stackoverflow.com/questions/28217436/how-to-show-an-empty-view-with-a-recyclerview) – Sanjay Hadiya May 21 '19 at 12:02

4 Answers4

6

You can handle the empty state by having a TextView in your layout, which switch its visibility state depending upon the array size you are getting in your response as:

JSONArray jsonArray = jsonObject.getJSONArray("data");
        if (jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject receive = jsonArray.getJSONObject(i);
                ExchangesSetterGetter exchangesSetterGetter = new ExchangesSetterGetter(
                        receive.getString("exchangeFrom"),
                        receive.getString("exchangeTo"),
                        receive.getString("status"),
                        receive.getString("imgSend"),
                        receive.getString("imgReceive"),
                        receive.getString("sendCurrency"),
                        receive.getString("receiveCurrency"),
                        receive.getString("amount_send"),
                        receive.getString("amount_receive")
                );
                listItems.add(exchangesSetterGetter);
            }
            adapter = new ExchangesAdapter(listItems, getContext());
            recyclerView.setAdapter(adapter);
            recyclerView.setVisibilty(View.VISIBLE);
            noDataText.setVisibilty(View.GONE);

        } else {
            recyclerView.setVisiblity(View.GONE);
            noDataText.setVisiblity(View.VISIBLE);
        }
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
0

There is a simple solution for that inside your fragment xml where recycler view is being loaded take a relative layout and add a textview with your desired message at last and set it's alignment as per your requirement and inside on create of your fragment set visiblity of your text view to View.INVISIBLE and later on after api is called simply pass an if condition stating that

if(response==null){ textview.setVisiblity = View.Visible}
Mr. Patel
  • 1,379
  • 8
  • 21
0

The way I like to handle this is to wrap your RecyclerView and your NoContentView (this can be whatever you like) in a ViewFlipper like so

<ViewFlipper> </RecyclerView> <NoContentView> </NoContentView> </ViewFlipper>

Then all you do is to check to see if you have list items and display the appropriate child layout of the ViewFlipper

if(listItems.size() == 0) { viewFlipper.setDisplayedChild(1) }

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
0

Add an extra textview in your fragment xml

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

    <TextView
        android:id="@+id/tv_no_data"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:textSize="22sp"
        android:visibility="gone" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_ride_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

And then in your activity

JSONArray jsonArray = jsonObject.getJSONArray("data");
if(jsonArray.length() > 0){
//process data and initialize adapter
}else{
//Set Text view visible
}
Arun
  • 321
  • 2
  • 8