我正在尝试制作一个应用程序,在其中使用Recycle View和Volley从服务器获取数据,并且我还使用了Navigation抽屉和片段,一切正常,但当在recycle-view上没有数据时,我想显示一个通知,例如“没有数据!”我多次搜索互联网,但没有找到合适的解决方案,或者因为我完全是初学者,所以无法正确理解。

波纹管是我的Java文件

1.适配器

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.查看架

    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(数据模型)

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.最后是我的带有回收视图的片段活动

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

最佳答案

在片段xml中添加一个额外的textview

<?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>


然后在你的活动中

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

10-06 12:15