首先,我必须告诉我我还在学习Android Studio,也许我想念了一些重要而又明显的职员。
现在我面临数据库问题,或者我不确定RecycleViwe。
我是我的应用程序,我正在尝试将数据插入db,仅在数据为空的情况下。我做到了,我认为这可行。 现在,我正在尝试实现一种新方法来读取和删除数据。现在,我可以在自己的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。当然,可以使用更简单的方式编写核心fore delete方法,但是我复制了一些最近的试用版。
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();
}
}
答案 0 :(得分:0)
您的删除方法是错误的。您正在传递参数 id ,但未将方法中的id用作
public void deleteCas(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Constants.TABLE_NAME, Constants.CAS + " = ?",
new String[] {});
db.close();
}
应该是
public void deleteCas(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Constants.TABLE_NAME, Constants.CAS + " = ?",
new String[] {id.toString()});
db.close();
}
语法未经测试