我正在从SQL迁移到ORM Room。我遵循了一个教程,它通常可以插入数据,但是删除项目不会消失。我必须打开并关闭该应用程序才能显示它已消失。
public class MainActivity extends AppCompatActivity implements Contrato.MainViewInterface {
@BindView(R.id.recyclerHome)
RecyclerView recyclerView;
private HomeAdapter adapter;
private List<Clientes> clientesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initViews();
loadNotes();
}
private void initViews() {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
}
private void loadNotes(){
ClienteRepository.getInstance(this).getNotes(this);
}
@OnClick(R.id.fabAdicionar)
public void addNote(){
Intent i = new Intent(MainActivity.this, AdicionarActivity.class);
startActivity(i);
}
@OnClick(R.id.buttonDeletar)
public void buttonDeletar(){
ClienteRepository.getInstance(this).deletarNotes();
}
@Override
public void onNotesLoaded(List<Clientes> clientesList) {
clientesList = clientesList;
if(clientesList.size() == 0){
onDataNotAvailable();
}else {
adapter = new HomeAdapter(this, clientesList);
recyclerView.setAdapter(adapter);
}
}
@Override
public void onDataNotAvailable() {
Toast.makeText(this,"Adicione uma nota",Toast.LENGTH_SHORT).show();
}
}
public class ClienteRepository {
private Context context;
private static ClienteRepository _instance;
private ClienteRoomDatabase db;
public static ClienteRepository getInstance(Context context) {
if (_instance == null) {
_instance = new ClienteRepository(context);
}
return _instance;
}
public ClienteRepository(Context context) {
this.context = context;
db = ClienteRoomDatabase.getClienteDatabase(context);
}
public void getNotes(final Contrato.MainViewInterface mainViewInterface) {
db.clienteDAO().getAllClientes().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<List<Clientes>>() {
@Override
public void accept(List<Clientes> notes) throws Exception {
mainViewInterface.onNotesLoaded(notes);
}
});
}
public void addNotes(final Contrato.AddNoteViewInterface addNoteViewInterface, final String nome) {
Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
Clientes clientes = new Clientes(nome);
db.clienteDAO().insert(clientes);
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io()).subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
addNoteViewInterface.onNoteAdded();
}
@Override
public void onError(Throwable e) {
addNoteViewInterface.onDataNotAvailable();
}
});
}
public void deletarNotes() {
Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
db.clienteDAO().deleteAll();
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io()).subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
});
}
}
@Dao
public interface ClienteDAO {
@Insert
void insert(Clientes clientes);
@Query("DELETE FROM table_clientes")
void deleteAll();
@Query("SELECT * FROM table_clientes ORDER BY name ASC")
Maybe<List<Clientes>> getAllClientes();
}
答案 0 :(得分:0)
从适配器的数据集中删除项目,然后调用adapter.notifyItemRemoved(position)