如何使用RecyClerview查看所有数据?

发布于 2025-02-06 05:15:46 字数 3169 浏览 3 评论 0原文

我无法将所有数据显示到recyclerview.i不断获得此错误:

引起: '在空对象参考上 在com.cdirect.weatherapp.mylocations.oncreate(mylocations.java:50)

mylocations.java:50:mylocations.java

rcView.setAdapter(adapter);

code:

public class MyLocations extends AppCompatActivity {

BottomNavigationView altNav;
RecyclerView rcView;
ArrayList<Locations> locationsList;
DatabaseReference dbRef;
Adapter adapter;

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(MyLocations.this,MainActivity.class));
    finish();
}

@Override



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dbRef = FirebaseDatabase.getInstance().getReference("Locations");
    altNav = (BottomNavigationView) findViewById(R.id.altNav);
    rcView = findViewById(R.id.rvLocations);

    locationsList = new ArrayList<>();
    adapter = new Adapter(MyLocations.this, locationsList);

    rcView.setAdapter(adapter); //ERROR LINE
    rcView.setLayoutManager(new LinearLayoutManager(this));

adapter.java code.java codes:

    public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    ArrayList<Locations> locationsArrayList;
    Context context;

    public Adapter (Context context, ArrayList<Locations> locationsArrayList){
        this.locationsArrayList=locationsArrayList;
        this.context = context;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.recycler_view_row,parent,false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Locations loc = locationsArrayList.get(position);
        holder.location.setText(loc.getLocation());
        holder.temperature.setText(loc.getTemp());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView location, temperature;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            location = itemView.findViewById(R.id.tvLocation);
            temperature= itemView.findViewById(R.id.tvTemp);
        }
    }
}

locations.java code.java code:

    public class Locations {
    String location;
    String temp;
    String id;

    public Locations(String location, String temp, String id) {
        this.location = location;
        this.temp = temp;
        this.id = id;
    }

    public String getLocation() {
        return location;
    }

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

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getId() {
        return id;
    }

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

我无法解决问题。你能帮助我吗?

I cannot show all datas to RecyclerView.I keep getting this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
at com.cdirect.weatherapp.MyLocations.onCreate(MyLocations.java:50)

MyLocations.java: 50:

rcView.setAdapter(adapter);

MyLocations.java code:

public class MyLocations extends AppCompatActivity {

BottomNavigationView altNav;
RecyclerView rcView;
ArrayList<Locations> locationsList;
DatabaseReference dbRef;
Adapter adapter;

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(MyLocations.this,MainActivity.class));
    finish();
}

@Override



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dbRef = FirebaseDatabase.getInstance().getReference("Locations");
    altNav = (BottomNavigationView) findViewById(R.id.altNav);
    rcView = findViewById(R.id.rvLocations);

    locationsList = new ArrayList<>();
    adapter = new Adapter(MyLocations.this, locationsList);

    rcView.setAdapter(adapter); //ERROR LINE
    rcView.setLayoutManager(new LinearLayoutManager(this));

Adapter.java codes:

    public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    ArrayList<Locations> locationsArrayList;
    Context context;

    public Adapter (Context context, ArrayList<Locations> locationsArrayList){
        this.locationsArrayList=locationsArrayList;
        this.context = context;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.recycler_view_row,parent,false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Locations loc = locationsArrayList.get(position);
        holder.location.setText(loc.getLocation());
        holder.temperature.setText(loc.getTemp());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView location, temperature;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            location = itemView.findViewById(R.id.tvLocation);
            temperature= itemView.findViewById(R.id.tvTemp);
        }
    }
}

Locations.java code:

    public class Locations {
    String location;
    String temp;
    String id;

    public Locations(String location, String temp, String id) {
        this.location = location;
        this.temp = temp;
        this.id = id;
    }

    public String getLocation() {
        return location;
    }

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

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getId() {
        return id;
    }

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

I cannot solve where is the problem. Can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文