首先,我必须告诉我我还在学习 Android Studio,也许我错过了一些重要且明显的员工。
现在我面临数据库问题,或者我不确定 RecycleViwe。
我的应用程序,我正在尝试将数据插入数据库,并且仅在数据为空的情况下。我做到了,我认为这很有效。现在我正在尝试实现读取数据和删除的新方法。现在我可以在我的 recycleView 中显示数据。
My problem come with delete method. When I start app, and when delete some items it is show like everything is fine. But when I start app again, data appears again like nothing was happened.
我不确定是什么导致了这个问题。希望你们中的一些人可以帮助我?
您将在下面找到部分代码。如果你想看更多,请随时问我。
In this part of code, I call my data and showing them in recycleView.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pondeljak = (Button) findViewById(R.id.buttonPonedeljak);
utorak = (Button) findViewById(R.id.buttonUtorak);
sreda = (Button) findViewById(R.id.buttonSreda);
cetvrtak = (Button) findViewById(R.id.buttonCetvrtak);
petak = (Button) findViewById(R.id.buttonPetak);
View view = getLayoutInflater().inflate(R.layout.casovi,null);
alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(view);
db = new DataDays(this);
alertDialog = alertDialogBuilder.create();
recyclerView = (RecyclerView) view.findViewById(R.id.recycleCasoviID);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
casoviList = new ArrayList<>();
casoviItems = new ArrayList<>();
casoviList = db.getCasoviPonedeljak();
for (Casovi c: casoviList){
Casovi casovi = new Casovi();
casovi.setRedniBrCasa(c.getRedniBrCasa());
casovi.setNzaivCasa(c.getNzaivCasa());
casoviItems.add(casovi);
}
recycleViewAdapter = new RecycleViewAdapter(this, casoviItems);
recyclerView.setAdapter(recycleViewAdapter);
recycleViewAdapter.notifyDataSetChanged();
pondeljak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (db.casoviCountPondeljak() == 0){
Intent intent = new Intent(MainActivity.this, Unos_casova.class);
intent.putExtra("Dan", "Pondeljak");
startActivity(intent);
}else {
alertDialog.show();
}
}
});
这是我的recycleView,还有我的deletemethod。当然,核心前删除方法可以用更简单的方式编写,但我复制了我最近的一些试验。
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
private Context context;
private List<Casovi> casoviItems;
public RecycleViewAdapter(Context context, List<Casovi> casoviItems) {
this.context = context;
this.casoviItems = casoviItems;
}
@Override
public RecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prikaz_casova, parent,false);
return new ViewHolder(view, context);
}
@Override
public void onBindViewHolder( RecycleViewAdapter.ViewHolder holder, int position) {
Casovi casovi = casoviItems.get(position);
holder.redniBrCasPrikaz.setText(casovi.getRedniBrCasa());
holder.nazivCasPrikaz.setText(casovi.getNzaivCasa());
}
@Override
public int getItemCount() {
return casoviItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView redniBrCasPrikaz;
public TextView nazivCasPrikaz;
public Button delteBtn;
public Button editBtn;
public CardView cardView;
public int id;
public ViewHolder(View view, Context ctx) {
super(view);
context = ctx;
redniBrCasPrikaz = (TextView) view.findViewById(R.id.rednibrojCasaID);
nazivCasPrikaz = (TextView) view.findViewById(R.id.nazivcasaID);
delteBtn = (Button) view.findViewById(R.id.obrisiCasBtnID);
editBtn = (Button) view.findViewById(R.id.izmeniCasBtnID);
cardView = (CardView) view.findViewById(R.id.carViewID);
delteBtn.setOnClickListener(this);
editBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.obrisiCasBtnID:
int position = getAdapterPosition();
Casovi casovi = casoviItems.get(position);
deleteItem(casovi.getId());
notifyItemRemoved(getAdapterPosition());
break;
}
}
public void deleteItem (final int id){
DataDays db = new DataDays(context);
db.deleteCas(id);
casoviItems.remove(getAdapterPosition());
}
}
}
这里是数据库的一些部分。正如我告诉你的那样,我不确定可能是什么问题,也许其中一些方法是以错误的方式编写的。
public void addCasovePonedeljak (Casovi casovi){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Constants.DAN, "Ponedeljak");
values.put(Constants.REDNI_CAS, casovi.getRedniBrCasa());
values.put(Constants.CAS, casovi.getNzaivCasa());
db.insert(Constants.TABLE_NAME, null, values);
}
public List<Casovi> getCasoviPonedeljak(){
SQLiteDatabase db = this.getReadableDatabase();
List<Casovi> casoviList = new ArrayList<>();
String pondeljakQuery = "SELECT * FROM " + Constants.TABLE_NAME + " WHERE " + Constants.DAN + " = " + " 'Ponedeljak' ";
Cursor cursor = db.rawQuery(pondeljakQuery, null);
if (cursor.moveToFirst()){
do {
Casovi casovi = new Casovi();
//casovi.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Constants.ID))));
//casovi.setDan(cursor.getString(cursor.getColumnIndex(Constants.DAN)));
casovi.setNzaivCasa(cursor.getString(cursor.getColumnIndex(Constants.CAS)));
casovi.setRedniBrCasa(cursor.getString(cursor.getColumnIndex(Constants.REDNI_CAS)));
casoviList.add(casovi);
}while (cursor.moveToNext());
}
return casoviList;
}
public void deleteCas(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Constants.TABLE_NAME, Constants.CAS + " = ?",
new String[] {});
db.close();
}
public int casoviCountPondeljak() {
String countQuery = "SELECT * FROM " + Constants.TABLE_NAME;
//String countQuery = "SELECT COUNT(*) FROM " + Constants.TABLE_NAME + " WHERE " + Constants.DAN + " = '" + "Ponedeljak' ";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery,null);
//cursor.moveToFirst();
return cursor.getCount();
}
}