Android 删除行onclick时出现房间数据库错误
我已将SQLite数据库定义为:Android 删除行onclick时出现房间数据库错误,android,android-sqlite,android-room,Android,Android Sqlite,Android Room,我已将SQLite数据库定义为: @Database(entities = {PlaceSaved.class},version = 1) public abstract class PlaceDatabase extends RoomDatabase { public abstract DatabaseInterface databaseInterface(); @Override protected SupportSQLiteOpenHelper createOp
@Database(entities = {PlaceSaved.class},version = 1)
public abstract class PlaceDatabase extends RoomDatabase {
public abstract DatabaseInterface databaseInterface();
@Override
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
return null;
}
@Override
protected InvalidationTracker createInvalidationTracker() {
return null;
}
}
定义见:
@Entity
public class PlaceSaved {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "time")
private String time;
@ColumnInfo(name="title")
private String title;
public PlaceSaved(){
}
public PlaceSaved(String time, String title) {
this.time = time;
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
相应的DAO是:
@Dao
public interface DatabaseInterface {
@Query("SELECT * FROM placesaved")
List<PlaceSaved> getAllItems();
@Insert
void insertAll(PlaceSaved... todoListItems);
@Delete
public void delete(PlaceSaved... todoListItems);
@Update
public void update(PlaceSaved...todoListItems);
}
recyclerview的名称为:(**注意:onCreate在末尾更新并发布)
它仍然给出语法错误:
Error:(71, 17) error: cannot find symbol method delete(PlaceSaved)
和2个警告(不是因为维舒的回答,它以前就存在过):
错误:更新:添加
db.databaseInterface().delete(infoItem)代码>
在removeitem中给出:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:164)
at android.arch.persistence.room.RoomDatabase.beginTransaction(RoomDatabase.java:211)
at DatabaseInterface_Impl.delete(DatabaseInterface_Impl.java:94)
at PlacesAdapter.removeItem(PlacesAdapter.java:69)
at PlacesAdapter.access$000(PlacesAdapter.java:20)
at PlacesAdapter$1.onClick(PlacesAdapter.java:45)
at android.view.View.performClick(View.java:6294)
为什么要在removitem()中传递PlaceSaved
的ArrayList
removietem()
仅接受PlaceSaved
因此,将您的onclick
更改为
removeItem(items.get(holder.getAdapterPosition()));
在方法签名中将holder
final-in-method-signaturefinal-PlacesAdapter.ViewHolder-holder
设置为final,否则它将无法编译
您已经在PlaceDatabase
中定义了delete
,并且正在调用PlaceSaved
,这就是您得到错误的原因:(68,17)错误:找不到符号方法delete(PlaceSaved)
您可以在PlacesAdapter
中传递db
,就像items
likePlacesAdapter(items,db)
改变
adapter = new PlacesAdapter(items);
到
现在,您的PlacesAdapter将具有db
实例。您可以回复placeSaved.delete(infoItem)代码>带有db.delete(infoItem)
改变
List<PlaceSaved> items;
public PlacesAdapter(List<PlaceSaved> items) {
this.items = items;
}
到
嗨,维苏,谢谢你的评论。但我还是不知道如何传递数据库。我已经更改了OnClick,它现在是有效的,但是removeItem方法仍然给出错误:错误:(69,17)错误:找不到符号方法delete(PlaceSaved)错误:任务执行失败:app:compiledBugJavaWithJavaC'>编译失败;有关详细信息,请参阅编译器错误输出。更新的答案!!维舒,再次感谢您的回复。更改PlacesAdapter(items,db)给出了错误:错误:(44,19)错误:类PlacesAdapter中的构造函数PlacesAdapter不能应用于给定的类型;必需:列表已找到:列表,位置数据库原因:实际参数列表和形式参数列表的长度不同更改构造函数,如answerHi中所述,这是非常重要的。现在,我的removietem看起来像:private void removietem(PlaceSaved infoItem){db.delete(infoItem);Log.v(TAG,“remove Item called”);}
没有错误,但也没有删除该项。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_layout);
final PlaceDatabase db = Room.databaseBuilder(getApplicationContext(), PlaceDatabase.class, "production")
.build();
Runnable r = new Runnable() {
@Override
public void run() {
items = db.databaseInterface().getAllItems();
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplication()));
adapter = new PlacesAdapter(items, db);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
};
Error:(71, 17) error: cannot find symbol method delete(PlaceSaved)
PlaceSaved.java
Warning:(11, 8) There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors.
PlaceDatabase.java
Warning:(13, 17) Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:164)
at android.arch.persistence.room.RoomDatabase.beginTransaction(RoomDatabase.java:211)
at DatabaseInterface_Impl.delete(DatabaseInterface_Impl.java:94)
at PlacesAdapter.removeItem(PlacesAdapter.java:69)
at PlacesAdapter.access$000(PlacesAdapter.java:20)
at PlacesAdapter$1.onClick(PlacesAdapter.java:45)
at android.view.View.performClick(View.java:6294)
removeItem(items.get(holder.getAdapterPosition()));
adapter = new PlacesAdapter(items);
adapter = new PlacesAdapter(items, db);
List<PlaceSaved> items;
public PlacesAdapter(List<PlaceSaved> items) {
this.items = items;
}
List<PlaceSaved> items;
PlaceDatabase db;
public PlacesAdapter(List<PlaceSaved> items, PlaceDatabase db) {
this.items = items;
this.db = db
}
placeSaved.delete(infoItem);
db.databaseInterface().delete(infoItem);