RecyclerView没有显示任何片段

时间:2018-09-30 21:18:49

标签: android android-recyclerview recycler-adapter

我正在尝试使用recyclerView从DB显示项目列表。但是,即使代码运行良好且没有任何错误,recyclerView也不会显示任何内容。可能是什么原因?如果有人可以帮助我的话。

我的代码如下:

RequestsAdapter.java

package com.bloodconnect.Fragments;

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

import com.bloodconnect.Model.Request;
import com.bloodconnect.R;

import java.util.List;

public class RequestsAdapter extends RecyclerView.Adapter<RequestsAdapter.RequestsViewHolder>{

    private Context mCtx;
    private List<Request> requestList;

    public RequestsAdapter(Context mCtx, List<Request> requestList) {
        this.mCtx = mCtx;
        this.requestList = requestList;

    }

    @Override
    public RequestsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.show_request_template, null);
        return new RequestsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RequestsViewHolder holder, int position) {
        Request requests = requestList.get(position);

        holder.request_name.setText(requests.getName());
        holder.request_blood_group.setText(requests.getBlood_group());
        holder.request_date.setText(String.valueOf(requests.getDate_of_request()));
        holder.request_address.setText(String.valueOf(requests.getAddress()));
        holder.request_lat_lng.setText(String.valueOf(requests.getLat_lng()));
    }


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

    class RequestsViewHolder extends RecyclerView.ViewHolder {

        TextView request_name, request_address, request_date, request_lat_lng, request_blood_group;

        public RequestsViewHolder(View itemView) {
            super(itemView);

            request_name = itemView.findViewById(R.id.show_request_name);
            request_blood_group = itemView.findViewById(R.id.show_request_bloodgroup);
            request_date = itemView.findViewById(R.id.show_request_date);
            request_address = itemView.findViewById(R.id.show_request_address);
            request_lat_lng = itemView.findViewById(R.id.show_request_location);
        }
    }

}

ShowRequestsFragment.java

package com.bloodconnect.Fragments;


import android.os.Bundle;
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 com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bloodconnect.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ShowRequestsFragment extends Fragment {

    private static final String URL_REQUESTS = "http://142.93.216.24/android/api.php?apicall=get_requests";
    List<com.bloodconnect.Model.Request> requestsList;
    RecyclerView recyclerView;

    public ShowRequestsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View rootView =  inflater.inflate(R.layout.fragment_show_requests, container, false);
        recyclerView = rootView.findViewById(R.id.listRecyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        //initializing the requestList
        requestsList = new ArrayList<>();

        loadRequests();

        //creating adapter object and setting it to recyclerview
        RequestsAdapter adapter;
        adapter = new RequestsAdapter(getActivity(), requestsList);
        recyclerView.setAdapter(adapter);
        return rootView;
    }

    private void loadRequests() {

        /*
         * Creating a String Request
         * The request type is GET defined by first parameter
         * The URL is defined in the second parameter
         * Then we have a Response Listener and a Error Listener
         * In response listener we will get the JSON response as a String
         * */
        StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, URL_REQUESTS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray();

                            //traversing through all the object
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject request = array.getJSONObject(i);

                                //adding the product to product list
                                requestsList.add(new com.bloodconnect.Model.Request(
                                        request.getString("name"),
                                        request.getString("blood_group"),
                                        request.getString("date_of_request"),
                                        request.getString("lat_lng"),
                                        request.getString("address")
                                ));
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(getContext()).add(stringRequest);
    }




}

我在一个扩展了NavigationDrawerActivity的片段中填充列表。

1 个答案:

答案 0 :(得分:0)

尝试在Adapter方法中接收到值之后立即设置onResponse,并按如下所示获取测试数据:

for (int i = 0; i < array.length(); i++) {
 JSONObject request = array.getJSONObject(i);

 String name = request.getString("name");

    requestsList.add(name)
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

这只是一个例子。我怀疑:

requestsList.add(new com.bloodconnect.Model.Request(
               request.getString("name"),
               request.getString("blood_group"),
               request.getString("date_of_request"),
               request.getString("lat_lng"),
               request.getString("address")
       ));

可能未将数据设置为requestsList


但是, 使用GSON并转换当前的json输出 是当今的最佳选择。