首页 > 解决方案 > 如果在第二个活动中使用,RecyclerView 在屏幕上不显示任何数据,但如果在启动器活动中使用,则显示列表

问题描述

在从第一个活动(RollDiceActivity.java)单击按钮时,我将进入第二个活动,我的 recyclerview 正在获取 JSON 对象(使用 Volley)。我的代码很简单:

viewQuotesBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // created explicit intent
        Intent intent = new Intent(RollDiceActivity.this,MainActivity.class);
        intent.putExtra("addedRandomNo", addedRandomNo);
        startActivity(intent);
        finish();
    }
});

第二个活动- MainActivity.java,recyclerView 在哪里,但 recyclerview 没有在屏幕上显示任何数据(但是 Log.d(TAG, response.toString() + "Size: "+response.length()); 给了我所有正在获取的 JSON 对象)

package com.subhasishlive.recyclerviewexplanation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

import Adapter.MyAdapter;
import Model.ListItem;
import Util.Constants;
import Util.Prefs;

/**
 * Created by SubhasishNath on 6/11/2018.
 */

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "getting length" ;
    private RecyclerView recyclerView;
    private MyAdapter adapter;
    private List<ListItem> listItems;// listitems are movies...
    private RequestQueue queue;// to use volleys

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // we are adding our queue and passing our current context....
            queue = Volley.newRequestQueue(MainActivity.this);

        // TODO: A FAB button would be added to call an alert dialog for search...


        // now we will set up our recyclerview and setup the adapter.
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
        recyclerView.setHasFixedSize(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        // creating an instance of Prefs class, calling parameterized constructor
        // passing our mainActivity as parameter...
        //Prefs prefs = new Prefs(MainActivity.this);
        // calling getSearch() method
        //String search = prefs.getSearch();

        // ArrayLists are capable of holding any type of lists
        listItems = new ArrayList<>();

        // listItems is set to the returning value of getMovies(), which is a List<ListItem>
        // so listItems holds all the movies returned by the search parameter.
        listItems = getMovies();

        adapter = new MyAdapter(MainActivity.this,listItems);// creating a new adapter for our listItems.
        recyclerView.setAdapter(adapter);//setting our adapter for our recyclerView.
        adapter.notifyDataSetChanged(); // Notify any registered observers that the data set has changed.
    }

    // creating a method to get movies...
    public List<ListItem> getMovies(){
        listItems.clear(); // first we're clearing the movie list....


        JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, Constants.base_url + "wp/v2/AllQuotes",new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                // TODO:response holds the whole JSON object from the URL
                try{
                    Log.d(TAG, response.toString() + "Size: "+response.length());
                    // instantiated a new JSON array.
                    // Pass the name of the JSON array as parameter.
                    // Search is name of the JSON array from the JSON file
                    //JSONArray moviesArray = response.getJSONArray("Search");
                    // Now I'm iterating through the array by a for loop
                    // every index-element in that array contains a JSON object.
                    for(int i=0;i<response.length();i++){
                        // instanciating JSONObject variable to pick a Index item(which is a JSON object) from the JSONArray.
                        //JSONObject movieObj =  moviesArray.getJSONObject(i);
                        ListItem movie = new ListItem();// creating ListItem object.
                        Log.d(TAG,"Object at " + i+ response.get(i));
                        // getting the JSONObject at i-th position of the JSONArray
                        JSONObject obj = response.getJSONObject(i);
                        // fetching id of each JSONObject from JSONArray.
                        movie.setId(obj.getInt("id"));

                        // title itself is an object so first I'm retrieving that
                        JSONObject titleObj=obj.getJSONObject("title");
                        // using title object I'm retrieving the string in it.
                        movie.setTitle(titleObj.getString("rendered"));

                        // content itself is an object so first I'm retriving that.
                        JSONObject contentObj = obj.getJSONObject("content");
                        // setting up the content
                        movie.setContent(contentObj.getString("rendered"));

                        // getting image object and setting up image.
                        JSONObject imgObj = obj.getJSONObject("better_featured_image");
                        movie.setPostImg(imgObj.getString("source_url"));


                        // getting excerpt object and setting up excerpt.
                        JSONObject excerptObj = obj.getJSONObject("excerpt");
                        movie.setPostExcerpt(excerptObj.getString("rendered"));


                        // adding the newly created movie to listItems list...
                        listItems.add(movie);
                    }

                }catch(JSONException e){
                    e.printStackTrace(); // throwing an exception
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO: Handle error

            }
        });

        queue.add(getRequest);
        return listItems;

    }
}

但是,如果我将第二个活动(MainActivity.java)更改为我在 AndroidManifest.xml 中的启动活动,那么它会在屏幕上显示所有 recyclerView 项目以及显示日志。

<activity
    android:name=".MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:label="@string/app_name"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

任何帮助将不胜感激。我很困惑,因为这个问题似乎很愚蠢。提前致谢 :)

标签: androidandroid-recyclerview

解决方案


您需要在列表adapter中添加数据后通知您listItems

尝试这个

在您的方法中的内部方法中adapter.notifyDataSetChanged(); 添加数据后使用listItemsonResponse()getMovies()

示例代码

public List<ListItem> getMovies(){
        listItems.clear(); // first we're clearing the movie list....


        JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, Constants.base_url + "wp/v2/AllQuotes",new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                // TODO:response holds the whole JSON object from the URL
                try{
                    Log.d(TAG, response.toString() + "Size: "+response.length());
                    // instantiated a new JSON array.
                    // Pass the name of the JSON array as parameter.
                    // Search is name of the JSON array from the JSON file
                    //JSONArray moviesArray = response.getJSONArray("Search");
                    // Now I'm iterating through the array by a for loop
                    // every index-element in that array contains a JSON object.
                    for(int i=0;i<response.length();i++){
                        // instanciating JSONObject variable to pick a Index item(which is a JSON object) from the JSONArray.
                        //JSONObject movieObj =  moviesArray.getJSONObject(i);
                        ListItem movie = new ListItem();// creating ListItem object.
                        Log.d(TAG,"Object at " + i+ response.get(i));
                        // getting the JSONObject at i-th position of the JSONArray
                        JSONObject obj = response.getJSONObject(i);
                        // fetching id of each JSONObject from JSONArray.
                        movie.setId(obj.getInt("id"));

                        // title itself is an object so first I'm retrieving that
                        JSONObject titleObj=obj.getJSONObject("title");
                        // using title object I'm retrieving the string in it.
                        movie.setTitle(titleObj.getString("rendered"));

                        // content itself is an object so first I'm retriving that.
                        JSONObject contentObj = obj.getJSONObject("content");
                        // setting up the content
                        movie.setContent(contentObj.getString("rendered"));

                        // getting image object and setting up image.
                        JSONObject imgObj = obj.getJSONObject("better_featured_image");
                        movie.setPostImg(imgObj.getString("source_url"));


                        // getting excerpt object and setting up excerpt.
                        JSONObject excerptObj = obj.getJSONObject("excerpt");
                        movie.setPostExcerpt(excerptObj.getString("rendered"));


                        // adding the newly created movie to listItems list...
                        listItems.add(movie);
                    }

                    adapter.notifyDataSetChanged();

                }catch(JSONException e){
                    e.printStackTrace(); // throwing an exception
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO: Handle error

            }
        });

        queue.add(getRequest);
        return listItems;

    }

推荐阅读