RecyclerView 中未显示默认数据

Default data not showing in RecyclerView

它没有显示我在 restoList 中设置的默认数据。我只是想按照 https://www.androidhive.info/2016/01/android-working-with-recycler-view/ 中的 Recycler View Example 我认为我的代码现在有些相同,但仍然无法正常工作。

package com.example.jmcervantes02.jan_24;

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

public class Recycler_Activity extends AppCompatActivity {

    private ArrayList<RestaurantModel> restoList = new ArrayList<>();
    private RecyclerView recyclerView;
    private RestaurantAdapter restoAdapter;

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

        recyclerView = (RecyclerView)findViewById(R.id.recycler_view);

        restoAdapter = new RestaurantAdapter(restoList);
        RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(rLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(restoAdapter);

        this.createDefaultRestaurants();
    }

    public void createDefaultRestaurants(){
        ArrayList<RestaurantModel> restoList = new ArrayList<RestaurantModel>();

        restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
        restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
        restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
        restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
        restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
        restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
        restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
        restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
        restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
        restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
        restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
        restoAdapter.notifyDataSetChanged();

        for(int i = 0; i < restoList.size(); i++){
            String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
            Log.v("Restaurant", restLog);
        }

        String count = String.valueOf(restoAdapter.getItemCount());
        Log.v("Count", count);

    }

}

适配器Class

package com.example.jmcervantes02.jan_24;

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

import junit.framework.Test;

import java.util.ArrayList;

public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.MyViewHolder> {
    private ArrayList<RestaurantModel> restoList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name, desc, weight;

        public MyViewHolder(View view){
            super(view);
            name = (TextView)view.findViewById(R.id.name_text);
            desc = (TextView)view.findViewById(R.id.desc_text);
            weight = (TextView)view.findViewById(R.id.weight_text);

        }
    }

    public RestaurantAdapter(ArrayList<RestaurantModel>restoList){
        this.restoList = restoList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_view, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position){
        RestaurantModel resto = restoList.get(position);
        holder.name.setText(resto.getName());
        holder.desc.setText(resto.getDescription());
        holder.weight.setText(resto.getWeight());
    }

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

}

主视图和回收器项目的 xml 也与示例相同

改代码如行云流水

 ArrayList<RestaurantModel> restoList; // declare global

设置值后调用适配器

    this.createDefaultRestaurants();
    restoAdapter = new RestaurantAdapter(restoList);
    RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(rLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(restoAdapter);

你的方法喜欢

   public void createDefaultRestaurants(){
    restoList= new ArrayList<RestaurantModel>();
    restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
    restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
    restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
    restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
    restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
    restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
    restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
    restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
    restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
    restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
    restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
    restoAdapter.notifyDataSetChanged();

    for(int i = 0; i < restoList.size(); i++){
        String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
        Log.v("Restaurant", restLog);
    }

    String count = String.valueOf(restoAdapter.getItemCount());
    Log.v("Count", count);

}

在您的 Recycler_Activity.java 中,根据您的代码,您首先调用 Adapter Constructor 然后将数据设置到您的 POJO/Model class,调用Adapter前设置数据为POJO.

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

    recyclerView = (RecyclerView)findViewById(R.id.recycler_view);

    this.createDefaultRestaurants();

    restoAdapter = new RestaurantAdapter(restoList);
    RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(rLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(restoAdapter);


}

对全局数组列表的引用

private ArrayList<RestaurantModel> restoList = new ArrayList<>();

和local arraylist不同

像这样从 createDefaultRestaurants 中删除 private ArrayList<RestaurantModel> restoList = new ArrayList<>();

public void createDefaultRestaurants(){
        restoList = new ArrayList<>();
        restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
        restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
        restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
        restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
        restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
        restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
        restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
        restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
        restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
        restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
        restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
        restoAdapter.notifyDataSetChanged();

        for(int i = 0; i < restoList.size(); i++){
            String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
            Log.v("Restaurant", restLog);
        }

        String count = String.valueOf(restoAdapter.getItemCount());
        Log.v("Count", count);

    }

无需在函数中重新创建 ArrayList<RestaurantModel> restoList 的对象 createDefaultRestaurants() 只需清除列表并重新分配即可

public void createDefaultRestaurants(){
    restoList.clear();
    restoList = new ArrayList<>();
    restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
    restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
   //
   // added model to list
   //
    restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
    restoAdapter.notifyDataSetChanged();

    for(int i = 0; i < restoList.size(); i++){
        String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
        Log.v("Restaurant", restLog);
    }

    String count = String.valueOf(restoAdapter.getItemCount());
    Log.v("Count", count);

}

您需要将新列表提供给适配器,然后调用 restoAdapter.notifyDataSetChanged();如果不向适配器提供新列表,notifyDataSetChanged 将不执行任何操作。

在adapter中添加一个函数,将list作为参数放入当前adapter列表中,然后调用notifydatasetchanged。

示例:在您的适配器中创建此功能

public void refreshList(List<Resto> restoList){
  this.restoList = restoList;
  notifyDataSetChanged();
}

代替restoAdapter.notifyDataSetChanged(); , 调用 restoAdapter.refreshList(restoList);