I've been trying for a few days but I can't display this RecyclerView. I also saw the tutorials of many sites, but unfortunately I still did not succeed.
I do not understand how the list should be taken from RingTonesDao and displayed in RecyclerView this is RingToneAdapter:
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;
}
}
this is 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);
}
}
and this is 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;
}
}
and this is 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();
}
CodePudding user response:
In adapter you don't set elements in to the list. Create method in adapter like setData()
and put into your list of elements. Then in onBindViewHolder()
set all values info xml fields.
CodePudding user response:
In your adapter creat a constructor with two inputs 1: context and 2: list you want to display and when you creating an adapter object in your activity , pass the context and list .