RecyclerView 最初不加载数据

Posted

技术标签:

【中文标题】RecyclerView 最初不加载数据【英文标题】:RecyclerView Does Not Load Data Initially 【发布时间】:2019-06-19 13:14:16 【问题描述】:

我在我的 android 项目的 Fragment 中使用 Spinner 和 RecyclerView。 在我在 Spinner 中选择一个项目之前,我的 RecyclerView 不会显示任何数据。 如果不从 Spinner 中选择项目,我应该怎么做才能显示 RecyclerView 的结果? 在第一个回收站应该显示 10 吸引力。

我的代码:

Attraction.java

package com.rayantec.reservation.reservationdemo.Model;

import android.app.ProgressDialog;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
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.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.rayantec.reservation.reservationdemo.Adapter.AttractionAdapter;
import com.rayantec.reservation.reservationdemo.G;
import com.rayantec.reservation.reservationdemo.Lists.AttractionList;
import com.rayantec.reservation.reservationdemo.Lists.CityList;
import com.rayantec.reservation.reservationdemo.R;
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;

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

import java.util.ArrayList;

public class Attractions extends Fragment 
    RecyclerView recyclerView;
    ArrayList<AttractionList> AttractionData =  new ArrayList<>();
    public SearchableSpinner spinnerCitiest;
    ArrayList<CityList> cityLists = new ArrayList<>();

    ProgressDialog progressDialog;


    public void showCity(View view) 
        spinnerCitiest = (SearchableSpinner) view.findViewById(R.id.SpinnerCitiestId);
        spinnerCitiest.setTitle("Please Select City");
        spinnerCitiest.setPositiveButton("Ok");
        //spinnerCitiest.setSelection(2);
        JsonArrayRequest JsonObjectRequest = new JsonArrayRequest(Request.Method.GET, G.serverURL + "/android/jsyncs/getdata/city", null, new Response.Listener<JSONArray>() 
            @Override
            public void onResponse(JSONArray response) 
                //progressDialog.dismiss();
                try 
                    for (int i = 0; i < response.length(); i++) 
                        JSONObject jsonOBJ = response.getJSONObject(i);
                        String id = jsonOBJ.getString("id");
                        String title = jsonOBJ.getString("title");
                        cityLists.add(new CityList(id, title));
                    
                 catch (JSONException e1) 
                    e1.printStackTrace();
                

            
        , new Response.ErrorListener() 
            @Override
            public void onErrorResponse(VolleyError error) 
                //progressDialog.dismiss();
                Toast.makeText(G.context, "Server Connection error", Toast.LENGTH_SHORT).show();
            
        );
        JsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(7000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestQueue requestQueue = Volley.newRequestQueue(G.context);
        requestQueue.add(JsonObjectRequest);

        ArrayAdapter<CityList> adapterCities = new ArrayAdapter<CityList>(G.context, android.R.layout.simple_spinner_item, cityLists);
        adapterCities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCitiest.setAdapter(adapterCities);

        spinnerCitiest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) 
                CityList cityList = (CityList) adapterView.getSelectedItem();
                Toast.makeText(G.context, "City ID: " + cityList.getId() + ",  City Name : " + cityList.getTitle(), Toast.LENGTH_SHORT).show();

            

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) 

            
        );
    

    public void showRandomAttractions(View view) 
        recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerAttracionId);
        JsonArrayRequest JsonObjectRequests = new JsonArrayRequest(Request.Method.GET, G.serverURL + "/android/jsyncs/getdata/randattraction", null, new Response.Listener<JSONArray>() 
            @Override
            public void onResponse(JSONArray response) 
                progressDialog.dismiss();
                //AttractionList sampledata = new AttractionList();
                try 
                    for (int i = 0; i < response.length(); i++) 
                        JSONObject jsonOBJs = response.getJSONObject(i);
                        String id = jsonOBJs.getString("id");
                        String title = jsonOBJs.getString("title");
                        String imageUrl = jsonOBJs.getString("imageUrl");
                        String location = jsonOBJs.getString("location");
                        String type = jsonOBJs.getString("type");
                        String body = jsonOBJs.getString("body");
                        AttractionData.add(new AttractionList(id, title, body, location, imageUrl, type));
                    
                 catch (JSONException e1) 
                    e1.printStackTrace();
                

            
        , new Response.ErrorListener() 
            @Override
            public void onErrorResponse(VolleyError error) 
                progressDialog.dismiss();
                Toast.makeText(G.context, "Server Connection error", Toast.LENGTH_SHORT).show();
            
        );
        JsonObjectRequests.setRetryPolicy(new DefaultRetryPolicy(7000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestQueue requestQueue = Volley.newRequestQueue(G.context);
        requestQueue.add(JsonObjectRequests);

        recyclerView.setLayoutManager(new LinearLayoutManager(G.context, LinearLayoutManager.VERTICAL, false));
        AttractionAdapter attractionAdapter = new AttractionAdapter(AttractionData);
        recyclerView.setAdapter(attractionAdapter);
    

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.attraction, container, false);

        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Please wait ...");
        progressDialog.setCancelable(false);
        progressDialog.show();


        showCity(view);
        showRandomAttractions(view);


        return view;
    

Attraction.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_>

    <android.support.v7.widget.CardView
        android:id="@+id/cardviewattraction"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:padding="3dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.toptoche.searchablespinnerlibrary.SearchableSpinner
            android:id="@+id/SpinnerCitiestId"
            android:layout_
            android:layout_
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:padding="3dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
             />
    </android.support.v7.widget.CardView>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerAttracionId"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cardviewattraction"
        tools:ignore="MissingConstraints" />


</android.support.constraint.ConstraintLayout>

景点适配器

package com.rayantec.reservation.reservationdemo.Adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rayantec.reservation.reservationdemo.Lists.AttractionList;
import com.rayantec.reservation.reservationdemo.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class AttractionAdapter extends RecyclerView.Adapter<AttractionAdapter.AttractionViewHolder> 
    ArrayList<AttractionList> attractionArrayList;

    public AttractionAdapter(ArrayList<AttractionList> attractions) 
        attractionArrayList = new ArrayList<>();
        attractionArrayList = attractions;
    

    @NonNull
    @Override
    public AttractionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_attraction, parent, false);
        return  new AttractionViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull AttractionViewHolder holder, int position) 
        AttractionList dataModel = attractionArrayList.get(position);
        holder.txtTitle.setText(dataModel.getTitle());
        holder.txtBody.setText(dataModel.getBody());
        holder.txtType.setText(dataModel.getType());
        holder.txtLocation.setText(dataModel.getLocation());
        Picasso.get().load(dataModel.getImgUrl()).resize(130, 100).centerCrop().into(holder.imgAttraction);
    

    @Override
    public int getItemCount() 
        return attractionArrayList.size();
    


    public class AttractionViewHolder extends RecyclerView.ViewHolder
        public ImageView imgAttraction;
        public TextView txtTitle;
        public TextView txtBody;
        public TextView txtLocation;
        public TextView txtType;

        public AttractionViewHolder(View itemView) 
            super(itemView);
            imgAttraction = (ImageView)itemView.findViewById(R.id.img_recycler_attraction);
            txtTitle = (TextView)itemView.findViewById(R.id.txt_recycler_attractionTitle);
            txtBody = (TextView)itemView.findViewById(R.id.txt_recycler_attractionBody);
            txtLocation = (TextView)itemView.findViewById(R.id.txt_recycler_attractionPositions);
            txtType = (TextView)itemView.findViewById(R.id.txt_recycler_attractionType);
        
        //
    

AttractionList.java

package com.rayantec.reservation.reservationdemo.Lists;

public class AttractionList 
    private String id;
    private String title;
    private String body;
    private String location;
    private String imgUrl;

    public AttractionList(String id, String title, String body, String location, String imgUrl, String type) 
        this.id = id;
        this.title = title;
        this.body = body;
        this.location = location;
        this.imgUrl = imgUrl;
        this.type = type;
    

    public String getType() 
        return type;
    

    public void setType(String type) 
        this.type = type;
    

    private String type;

    public String getId() 
        return id;
    

    public void setId(String id) 
        this.id = id;
    

    public String getTitle() 
        return title;
    

    public void setTitle(String title) 
        this.title = title;
    

    public String getBody() 
        return body;
    

    public void setBody(String body) 
        this.body = body;
    

    public String getLocation() 
        return location;
    

    public void setLocation(String location) 
        this.location = location;
    

    public String getImgUrl() 
        return imgUrl;
    

    public void setImgUrl(String imgUrl) 
        this.imgUrl = imgUrl;
    

【问题讨论】:

代码无法通过替换此方法正常工作。 【参考方案1】:

您应该测试一些东西,您是否检查过您的showRandomAttractions(View view) 方法中是否收到了onResponse 回调?这将验证 Get 调用是否按预期工作。

如果您收到onResponse 调用,请在您完成将数据添加到AttractionData ArrayList 后尝试在for 循环之后调用attractionAdapter.notifyDataSetChanged()。它看起来像这样:

            try 
                for (int i = 0; i < response.length(); i++) 
                    JSONObject jsonOBJs = response.getJSONObject(i);
                    String id = jsonOBJs.getString("id");
                    String title = jsonOBJs.getString("title");
                    String imageUrl = jsonOBJs.getString("imageUrl");
                    String location = jsonOBJs.getString("location");
                    String type = jsonOBJs.getString("type");
                    String body = jsonOBJs.getString("body");
                    AttractionData.add(new AttractionList(id, title, body, location, imageUrl, type));

                    // Notify your adapter that its data has changed.
                    attractionAdapter.notifyDataSetChanged();
                
             catch (JSONException e1) 
                e1.printStackTrace();
            

您可以查看此documentation,了解有关notifyDataSetChanged 工作原理的更多信息。

【讨论】:

感谢回答,在循环后创建吸引适配器(在代码末尾),我不能使用吸引适配器.notifyDataSetChanged();在 for 循环之后,我在创建吸引适配器后使用此代码,但代码也不起作用。所有代码都运行良好,意味着从服务器接收数据并进行解析。但是当打开的片段数据不显示并且在我选择任何城市后,将显示回收站视图。【参考方案2】:

我相信您对服务器收集数据的请求是 Asynchronous 并且发生在 Worker Thread 上。如果是这种情况,那么您的视图会在实际获取数据之前呈现。

我建议在您的适配器中添加一个setData 函数:

public void setData(ArrayList<Attractions> attractions)
        this.attractionArrayList = attractions
        notifyDataSetChanged()

然后在您的活动上,当您收到响应并构建吸引力数组时,只需致电adapter.setData(attractions)

更新: 确保在视图实际初始化时设置您的适配器,因此您需要在 onViewCreated 上调用它:

public void onViewCreated(View view,Bundle savedInstanceState) 
    super.onViewCreated(view, savedInstanceState);
    showCity(view);
    showRandomAttractions(view);

【讨论】:

感谢您的回答,我对其进行了测试,但尚未运行,代码稍后运行,例如,如果我选择第一个城市回收站,则稍后显示 10 吸引力,然后我选择第二个城市和回收商展示与第一个城市相关的景点,这是上一步的【参考方案3】:

在 try/catch 这个问题解决后替换这一行

recyclerView.setLayoutManager(new LinearLayoutManager(G.context, LinearLayoutManager.VERTICAL, false));
            AttractionAdapter attractionAdapter = new AttractionAdapter(AttractionData);
            recyclerView.setAdapter(attractionAdapter);

【讨论】:

以上是关于RecyclerView 最初不加载数据的主要内容,如果未能解决你的问题,请参考以下文章

Android Recycer 流式布局

加载 Firebase 实时数据库时的 ProgressBar [重复]

使用单击 Spinner 项刷新 ArrayList 以将新数据显示到 RecyclerView

如何在 RecyclerView 中实现 StartActivityForResult

RecyclerView 挤压子项宽度

为啥 recyclerview 的 searchview 不起作用?