简体   繁体   English

RecyclerView 未加载

[英]RecyclerView not loading

I have created a Recycler view that is supposed to be created when the activity is created.我创建了一个 Recycler 视图,该视图应该在创建活动时创建。 Currently, when I click a button on my MainActivity, an intent launches the ListActivity which has my recyclerview but it doesn't load.目前,当我单击 MainActivity 上的按钮时,意图会启动具有我的 recyclerview 但未加载的 ListActivity。 I have used toast message to confirm that each method is getting called, and that I am getting the correct data from the API.我已经使用 toast 消息来确认每个方法都被调用,并且我从 API 获得了正确的数据。 If I reset the activity using the restart activity option in Android Studio the Recycler shows up and functions correctly.如果我使用 Android Studio 中的重新启动活动选项重置活动,则 Recycler 会显示并正常运行。 I don't know what I'm doing wrong.我不知道我做错了什么。

Here is my ListActivity:这是我的列表活动:


    private RecyclerView myrecyclerview;
    private RecyclerView.Adapter myadapter;
    private RecyclerView.LayoutManager mylayoutmanager;
    static RequestQueue listqueue;
    static final private String url = "https://swapi.dev/api/people/";

    static ArrayList<RecyclerItem> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        getSupportActionBar().hide();
        listqueue = Volley.newRequestQueue(this);

        myrecyclerview = findViewById(R.id.characterlist);
        

        myadapter = new MyAdapter(list, this);
        myrecyclerview.setAdapter(myadapter);
        myrecyclerview.setHasFixedSize(true);
        mylayoutmanager = new LinearLayoutManager(getApplicationContext());
        myrecyclerview.setLayoutManager(mylayoutmanager);
        parseJsonData();
    }

    public void parseJsonData(){
    JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.GET,
            url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();

                    try {
                        JSONArray jsonarray = response.getJSONArray("results");
                        for (int i = 0; i < jsonarray.length(); i++) {
                            JSONObject jsonobject = jsonarray.getJSONObject(i);

                            String name = jsonobject.getString("name");
                            String height = jsonobject.getString("height");
                            String mass = jsonobject.getString("mass");
                            String eyecolor = jsonobject.getString("eye_color");
                            String birthyear = jsonobject.getString("birth_year");

                            //list.add(new RecyclerItem("darth vader", "200", "128", "1950", "red"));
                            list.add(new RecyclerItem(name, "Height: " + height, "Mass: " + mass, "Birth Year: " + birthyear, eyecolor));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

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

        listqueue.add(request);

    }


    @Override
    public void onCharacterClick(int position) {
        String color = list.get(position).getEyecolor();
        Toast.makeText(getApplicationContext(), color, Toast.LENGTH_SHORT).show();
    }

} ```

Like I mentioned, once I reload the activity, it works correctly. But I want the recycler view to show when I navigate to the activity.

The problem is that the first time that the activity is create, list is empty and parseJsonData() is running on the background filling that list.问题是第一次创建活动时, list为空,并且parseJsonData()正在后台运行填充该列表。 Once you reload the activiy, the list and adapter are filled therefore when you call重新加载活动后,列表和适配器将被填充,因此当您调用时

myadapter = new MyAdapter(list, this);
myrecyclerview.setAdapter(myadapter);
myrecyclerview.setHasFixedSize(true);
mylayoutmanager = new LinearLayoutManager(getApplicationContext());
myrecyclerview.setLayoutManager(mylayoutmanager);

the recycler view is show.显示回收站视图。 Try to do this on your parseJsonData() ;尝试在您的parseJsonData()上执行此操作; after the loop ends, then create the adapter and show the rv循环结束后,创建适配器并显示 rv

for (int i = 0; i < jsonarray.length(); i++) {
   JSONObject jsonobject = jsonarray.getJSONObject(i);
   ....
}

myadapter = new MyAdapter(list, this);
....
myrecyclerview.setLayoutManager(mylayoutmanager);

I hope it help for u我希望它对你有帮助

Creat a List创建列表

private List< TipsList > tipsLists = new ArrayList<>();

SET UP RECYCLERVIEW LIKE THIS像这样设置回收站视图

RecyclerView tipsRv = findViewById(R.id.tips_rv);
    TipsAdapter adapter = new TipsAdapter(tipsLists, this);
    tipsRv.setAdapter(adapter);
    tipsRv.setHasFixedSize(true);
    tipsRv.setLayoutManager(new LinearLayoutManager(this));

getDATA获取数据

 public void getWallis() {
    String myJSONStr = method.loadJSON();

    try {
        JSONObject ROOT_OBJ = new JSONObject(myJSONStr);
        JSONArray MAIN_ARRAY = ROOT_OBJ.getJSONArray("ff_api");
        JSONObject TIPS_OBJ = MAIN_ARRAY.getJSONObject(3);
        JSONArray TIPS_ARRAY = TIPS_OBJ.getJSONArray("Tips");

        for (int i = 0; i < TIPS_ARRAY.length(); i++) {
            TipsList tipsList = new TipsList();
            JSONObject jsonObject = TIPS_ARRAY.getJSONObject(i);
            tipsList.setId(jsonObject.getInt("id"));
            tipsList.setTipsTitle(jsonObject.getString("tipsTitle"));
            tipsList.setTipsDec(jsonObject.getString("tipsDec"));
            tipsLists.add(tipsList);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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