我无法更新数据库室中的项目列表并将项目添加到收藏夹列表
I can not update an item list in the database room and add the item to the favorites list
我无法通过单击“添加到收藏夹”来设置 isfavorites 值。
Android Studio 显示 ringTonesDao 的空指针异常。
我不知道如何解决这个错误。
另一方面,因为ringTonesDao是一个接口,如果我将它的值设置为new RingTonesDao,我必须设置它的所有功能。
这是 RingTonesAdapter
//Imports and Package name
public class RingTonesAdapter extends
RecyclerView.Adapter<RingTonesAdapter.ringTonesViewHolder> {
private List<RingTones> ringTonesList;
RingTonesDao ringTonesDao; // <--This is null in logcat
public RingTonesAdapter(List<RingTones> ringTonesList) {
this.ringTonesList = ringTonesList;
}
@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) {
//Set RingTone name to TextView
//Set RingTone Btn
//Set Alarm Btn
//Set Notification Btn
//Add To Favorites Btn
if (ringTonesList.get(position).isFavorite()) {
holder.btn_addToFavorites.setImageResource(R.drawable.ic_favorites);
} else {
holder.btn_addToFavorites.setImageResource(R.drawable.ic_addtofavorites);
}
holder.btn_addToFavorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Long id = ringTonesList.get(position).getId();
ringTonesDao.update(true,id);
}catch (NullPointerException e){
Toast.makeText(v.getContext(),"Null Pointer
Exception",Toast.LENGTH_LONG).show();
}
}
});
//Set Play Btn
//ringTonesViewHolder and count items
这是 RingTonesDao
//import and package
@Dao
public interface RingTonesDao {
@Insert
Long add(RingTones ringTones);
@Delete
int delete(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();
@Query("UPDATE tbl_ringtones SET favorites=:favorites WHERE id=:id")
int update (Boolean favorites ,Long id);
}
你需要做这样的事情
DatabaseClient.getInstance(v.getContext()).getAppDatabase()
.RingTonesDao()
.update(true,id);
阅读更多如何使用 Rom 数据库:from this example
Java中的Boolean和boolean也有区别,
在您的情况下,请尝试在更新函数中使用 `boolean` 而不是 `Boolean`。
我无法通过单击“添加到收藏夹”来设置 isfavorites 值。 Android Studio 显示 ringTonesDao 的空指针异常。 我不知道如何解决这个错误。 另一方面,因为ringTonesDao是一个接口,如果我将它的值设置为new RingTonesDao,我必须设置它的所有功能。
这是 RingTonesAdapter
//Imports and Package name
public class RingTonesAdapter extends
RecyclerView.Adapter<RingTonesAdapter.ringTonesViewHolder> {
private List<RingTones> ringTonesList;
RingTonesDao ringTonesDao; // <--This is null in logcat
public RingTonesAdapter(List<RingTones> ringTonesList) {
this.ringTonesList = ringTonesList;
}
@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) {
//Set RingTone name to TextView
//Set RingTone Btn
//Set Alarm Btn
//Set Notification Btn
//Add To Favorites Btn
if (ringTonesList.get(position).isFavorite()) {
holder.btn_addToFavorites.setImageResource(R.drawable.ic_favorites);
} else {
holder.btn_addToFavorites.setImageResource(R.drawable.ic_addtofavorites);
}
holder.btn_addToFavorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Long id = ringTonesList.get(position).getId();
ringTonesDao.update(true,id);
}catch (NullPointerException e){
Toast.makeText(v.getContext(),"Null Pointer
Exception",Toast.LENGTH_LONG).show();
}
}
});
//Set Play Btn
//ringTonesViewHolder and count items
这是 RingTonesDao
//import and package
@Dao
public interface RingTonesDao {
@Insert
Long add(RingTones ringTones);
@Delete
int delete(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();
@Query("UPDATE tbl_ringtones SET favorites=:favorites WHERE id=:id")
int update (Boolean favorites ,Long id);
}
你需要做这样的事情
DatabaseClient.getInstance(v.getContext()).getAppDatabase()
.RingTonesDao()
.update(true,id);
阅读更多如何使用 Rom 数据库:from this example
Java中的Boolean和boolean也有区别,
在您的情况下,请尝试在更新函数中使用 `boolean` 而不是 `Boolean`。