我正在尝试创建一个使用Recycle View和Volley从服务器获取数据的应用程序,并且我使用了Navigation抽屉和片段,一切正常,除了当recycle-view上没有数据时,我想显示一个“没有数据!”我多次搜索互联网,但没有找到合适的解决方案,或者因为我完全是新手,所以无法正确理解。
bellow是我的java文件
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(数据模型)
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);
}
}
答案 0 :(得分:6)
您可以通过在布局中放置一个TextView来处理空状态,TextView会根据您在响应中得到的数组大小来切换其可见性状态:
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);
}
答案 1 :(得分:0)
在片段XML内部有一个简单的解决方案,其中加载了回收器视图,采用相对布局,最后添加带有所需消息的textview,并根据您的要求并在创建片段集时在内部设置其对齐方式您的文本视图对View.INVISIBLE的可见性,以及在调用api之后,只需传递一个if条件,说明该
if(response==null){ textview.setVisiblity = View.Visible}
答案 2 :(得分:0)
我喜欢的处理方式是将RecyclerView
和NoContentView
(可以是任意的)包装在ViewFlipper
中,
<ViewFlipper>
</RecyclerView>
<NoContentView>
</NoContentView>
</ViewFlipper>
然后您要做的就是检查是否有列表项,并显示ViewFlipper
if(listItems.size() == 0)
{
viewFlipper.setDisplayedChild(1)
}
答案 3 :(得分:0)
在片段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
}