如何在 RecyclerView 中显示这个列表

How to display this list in RecyclerView

我已经试了几天了,还是无法显示这个RecyclerView。我也看了很多网站的教程,可惜还是没有成功

我不明白该列表应该如何从 RingTonesDao 中获取并显示在 RecyclerView 中 这是铃声适配器:

package com.example.ringtone2021;
      
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.List;

public class RingTonesAdapter extends RecyclerView.Adapter<RingTonesAdapter.ringTonesViewHolder> {
    private RingTonesDao ringTonesDao;
    private List<RingTones> ringTonesList;

    public class ringTonesViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_musicname;
        private ImageView btn_ringtone;
        private ImageView btn_sms;
        private ImageView btn_alarm;
        private ImageView btn_play;
        private ImageView btn_addToFavorites;

        public ringTonesViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_musicname = itemView.findViewById(R.id.tv_musicName);
            btn_ringtone = itemView.findViewById(R.id.iv_ringTone);
            btn_sms = itemView.findViewById(R.id.iv_sms);
            btn_alarm = itemView.findViewById(R.id.iv_alarm);
            btn_play = itemView.findViewById(R.id.iv_play);
            btn_addToFavorites = itemView.findViewById(R.id.iv_favorite);
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull ringTonesViewHolder holder, int position) {
       // holder.tv_musicname.setText(ringTonesList.get(position).getMusicNames());
    }
    @Override
    public int getItemCount() {
        return 30;
    }
}

这是 MainActivity:

 package com.example.ringtone2021;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;


public class MainActivity extends AppCompatActivity {
    private RingTonesDao ringTonesDao;
    private RecyclerView recyclerView;
    private RingTonesAdapter ringTonesAdapter = new RingTonesAdapter();
    private TextView forexample;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        forexample = findViewById(R.id.forexample);
        recyclerView = findViewById(R.id.rv_main);
        recyclerView.setAdapter(ringTonesAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

        ringTonesDao = AppDatabase.getAppDatabase(this).getRingtonesDao();

        RingTones ringTones = new RingTones("reza", "alavi");
        ringTonesDao.add(ringTones);
        ringTones = new RingTones("ali", "alavi");
        ringTonesDao.add(ringTones);
        ringTones = new RingTones("ali", "alavi");
        ringTonesDao.add(ringTones);
        ringTones = new RingTones("ali", "alavi");
        ringTonesDao.add(ringTones);
        ringTones = new RingTones("ali", "alavi");
        ringTonesDao.add(ringTones);
        ringTones = new RingTones("ali", "alavi");
        ringTonesDao.add(ringTones);
    }
}

这是铃声:

package com.example.ringtone2021;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

@Entity(tableName = "tbl_ringtones")
public class RingTones {
    @PrimaryKey(autoGenerate = true)
    private Long id;
    private String musicNames;
    private String musicAddress;
    @ColumnInfo(name = "favorites")
    private boolean isFavorite;

    public RingTones(String musicNames, String musicAddress) {
        this.musicNames = musicNames;
        this.musicAddress = musicAddress;
        this.isFavorite = false;
    }

    public Long getId() {
        return id;
    }

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

    public String getMusicNames() {
        return musicNames;
    }

    public void setMusicNames(String musicNames) {
        this.musicNames = musicNames;
    }

    public String getMusicAddress() {
        return musicAddress;
    }

    public void setMusicAddress(String musicAddress) {
        this.musicAddress = musicAddress;
    }

    public boolean isFavorite() {
        return isFavorite;
    }

    public void setFavorite(boolean favorite) {
        isFavorite = favorite;
    }
    
}

这是 RingTonesDao:

    package com.example.ringtone2021;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface RingTonesDao {
    @Insert
    Long add(RingTones ringTones);

    @Delete
    int delete(RingTones ringTones);

    @Update
    int update(RingTones ringTones);

    @Query("SELECT * FROM tbl_ringtones")
    List<RingTones> getRingTones();

    @Query("SELECT * FROM tbl_ringtones WHERE favorites>0 ")
    List<RingTones> favorites();

    @Query("DELETE FROM tbl_ringtones")
    void deleteAll();

}

在适配器中,您没有将元素设置到列表中。在适配器中创建方法,如 setData() 并放入您的元素列表中。然后在 onBindViewHolder() 中设置所有值信息 xml 字段。

在您的适配器中创建一个构造函数,其中包含两个输入 1:上下文和 2:您要显示的列表,当您在 activity 中创建适配器对象时,传递上下文和列表。