简体   繁体   中英

RecyclerView data empty to display textview in fragment

My json data is empty to visible the textview and hide the recyclerview.
My xml code recyclerview and 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"
    />



My java code show textview & hide recyclerview here:

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

It's possible to any one help me.I am new in android code.

In your XML, you have set the visibility of your TextView "pendingnullorder" to "gone" and in the onCreateView method, you have also set the visibility of your RecyclerView "pending" to View.GONE . It seems you see none of your views.

In your method getProgressData (or getPendingData ) you should change the visibility of one of this view depending on the size of your adapter (if no element: pendingnullorder.setVisibility(View.VISIBLE); , otherwise: recyclerView.setVisibility(View.VISIBLE); ), and update visibilities on the onResponse method.

[EDIT]

Here is an example of what you can do:

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(inflater.getContext()));

    orderLists = new ArrayList<>();

    getPendingData();

    adapter = new OrderListAdapter(orderLists, inflater.getContext());
    recyclerView.setAdapter(adapter);

    updateUI();

    return drawer;
}

private void getPendingData() {
    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();
    updateUI();
}

private void updateUI() {
    if (adapter.getCount() == 0) {
        pendingnullorder.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    } else {
        pendingnullorder.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
    }
}
  1. First you need to check the data is not empty.
  2. Second we can optimize the way to show the data for RecycleView. Remove

    adapter = new OrderListAdapter(orderLists, getActivity()); recyclerView.setAdapter(adapter);

in the onCreateView method. In progressOrderProgress method, please do as below

   if(adapter==null){
        adapter = new OrderListAdapter(orderLists, getActivity());
        recyclerView.setAdapter(adapter);
   } else {
        adapter.updateData(orderLists); // please define this method in your adapter
        adapter.notifyDataSetChanged();
   }

Hope it will resolve your issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM