Я создаю приложение todo с SQLite и RecyclerView, но у меня возникают проблемы при удалении задачи по щелчку изображения, которое является моим ivDelete.Я хочу удалить выполненные задачи.есть идеи как удалить?любая помощь будет оценена.
Это мой MainActivity:
public class MainActivity extends AppCompatActivity
{
Button btn_tasks,btn_reminders,btn_about;
ImageView img_addtask;
RecyclerView contactView;
DBHelper dbHelper;
CustomAdapter mAdapter;
private ArrayList<TaskClass> allContacts;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_tasks = findViewById(R.id.btn_task);
btn_reminders = findViewById(R.id.btn_reminder);
btn_about = findViewById(R.id.btn_about);
img_addtask = findViewById(R.id.imageView);
dbHelper = new DBHelper(this);
contactView = findViewById(R.id.list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
contactView.setLayoutManager(layoutManager);
contactView.setHasFixedSize(true);
allContacts = new ArrayList<>();
allContacts = dbHelper.getTaskList();
loadTaskList();
img_addtask.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
addTask();
}
});
btn_reminders.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
addReminder();
}
});
}
private void loadTaskList()
{
ArrayList<TaskClass> taskList = dbHelper.getTaskList();
if(mAdapter == null)
{
contactView.setVisibility(View.VISIBLE);
mAdapter = new CustomAdapter(this,allContacts);
contactView.setAdapter(mAdapter);
}
else
{
contactView.setVisibility(View.GONE);
Toast.makeText(this, "There is no task in the database. Start
adding now", Toast.LENGTH_LONG).show();
}
}
private void addTask()
{
Intent intent = new Intent(this,AddTask.class);
startActivity(intent);
}
private void addReminder()
{
Intent intent = new Intent(this,AddReminder.class);
startActivity(intent);
}
}
Это мой класс DBHelper:
public class DBHelper extends SQLiteOpenHelper
{
private static final String DB_NAME = "Demo.db";
private static final int DB_VER = 1;
private static final String DB_TABLE = "Task";
DBHelper(Context context)
{
super(context, DB_NAME, null, DB_VER);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(" CREATE TABLE " +DB_TABLE+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, TaskName TEXT, dateStr TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1)
{
db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE);
onCreate(db);
}
void insertNewTask(TaskClass taskClass)
{
ContentValues values = new ContentValues();
SQLiteDatabase db= this.getWritableDatabase();
values.put("TaskName",taskClass.getTaskName());
values.put("dateStr", taskClass.getTaskDate());
db.insert(DB_TABLE,null,values);
db.close();
}
public ArrayList<TaskClass> getTaskList()
{
String selectQuery = "SELECT * FROM " + DB_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<TaskClass> taskList = new ArrayList<>();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do {
String name = cursor.getString(cursor.getColumnIndex("TaskName"));
String date = cursor.getString(cursor.getColumnIndex("dateStr"));
taskList.add(new TaskClass(name,date));
}while (cursor.moveToNext());
}
cursor.close();
return taskList;
}
}
Это класс CustomAdapter:
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new CustomViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CustomViewHolder viewHolder, final int position)
{
viewHolder.txtTask.setText(taskClass.get(position).getTaskName());
viewHolder.txtDate.setText(taskClass.get(position).getTaskDate());
}
@Override
public int getItemCount() {
return taskClass.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder
{
TextView txtTask,txtDate;
ImageView ivDelete;
public CustomViewHolder(View itemView)
{
super(itemView);
txtTask = itemView.findViewById(R.id.task_title);
txtDate = itemView.findViewById(R.id.task_date);
ivDelete = itemView.findViewById(R.id.ivDelete);
}
}