我的json数据为空,可见textview并隐藏Recyclerview
我的xml代码recyclerview和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"
android:padding="@dimen/common_padding"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/pending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
<TextView
android:id="@+id/pendingnullorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current no orders available"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="@dimen/order_list_view_font_size"
android:visibility="gone"
/>
我的java代码显示textview&amp;在这里隐藏recyclerview:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View drawer = inflater.inflate(R.layout.fragment_pending, container, false);
pendingnullorder = (TextView) drawer.findViewById(R.id.pendingnullorder);
recyclerView = (RecyclerView) drawer.findViewById(R.id.pending);
progressBar = (ProgressBar) drawer.findViewById(R.id.progressBar);
recyclerView.setVisibility(View.GONE);
progressBar.setVisibility(ProgressBar.VISIBLE);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
orderLists = new ArrayList<>();
getProgressData();
adapter = new OrderListAdapter(orderLists, getActivity());
recyclerView.setAdapter(adapter);
return recyclerView;
}
private void getProgressData(){
String token = SharedPreferencesManager.readPreferenceString("token", "D/N");
JSONObject progressData = new JSONObject();
try{
progressData.put("token", token);
JsonObjectRequest progressObject = new JsonObjectRequest(1, Common.OrderDetails + "progress", progressData, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject progressResponse) {
Log.d("Responseprogress", progressResponse.toString());
try {
int status = progressResponse.getInt("status");
if(status == 1) {
progressOrderProgress(progressResponse);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("Response", "PROGRESS ERROR");
}
});
progressObject.setShouldCache(false);
ServiceBellApp.getInstance().addToRequestQueue(progressObject);
}
catch (JSONException localJSONException){
localJSONException.printStackTrace();
return;
}}private void progressOrderProgress(JSONObject progressResponse) throws JSONException {
JSONArray result = progressResponse.getJSONArray("orderdata");
for(int i=0; i<result.length(); i++){
OrderList orderListModule = new OrderList();
JSONObject orderData = null;
try {
orderData = result.getJSONObject(i);
orderListModule.setPackage_name(orderData.getString("name"));
orderListModule.setOrderdate(orderData.getString("date"));
orderListModule.setServicedate(orderData.getString("service"));
orderListModule.setServicetime(orderData.getString("time"));
orderListModule.setOrderid(orderData.getString("id"));
orderListModule.setOrdstatus(orderData.getString("status"));
orderListModule.setOrderamount(orderData.getInt("ramount"));
}catch (JSONException e) {
e.printStackTrace();
}
orderLists.add(orderListModule);
}
swipeRefreshLayout.setRefreshing(false);
adapter.notifyDataSetChanged();}
任何人都可以帮助我。我是Android代码的新手。
答案 0 :(得分:2)
在您的XML中,您已将TextView
"pendingnullorder"
的可见性设置为"gone"
,并且在onCreateView
方法中,您还设置了{{{1}的可见性1}} RecyclerView
到"pending"
。你似乎没有看到任何观点。
在方法View.GONE
(或getProgressData
)中,您应根据适配器的大小更改此视图之一的可见性(如果没有元素:getPendingData
,否则:{ {1}}),并更新pendingnullorder.setVisibility(View.VISIBLE);
方法的可见性。
<强> [编辑] 强>
以下是您可以执行的操作示例:
recyclerView.setVisibility(View.VISIBLE);
答案 1 :(得分:1)
其次,我们可以优化显示RecycleView数据的方式。 删除
adapter = new OrderListAdapter(orderLists, getActivity());
recyclerView.setAdapter(adapter);
onCreateView
方法中的。在progressOrderProgress
方法中,请按以下方式执行
if(adapter==null){
adapter = new OrderListAdapter(orderLists, getActivity());
recyclerView.setAdapter(adapter);
} else {
adapter.updateData(orderLists); // please define this method in your adapter
adapter.notifyDataSetChanged();
}
希望它能解决您的问题