У меня есть ListView, который я обновляю из своей базы данных SQLite, и я установил onItemLongClickListener, чтобы помочь мне удалить из моей базы данных через диалоговый интерфейс.Однако у меня не может быть доступа к notifyDataSetChanges, даже если я сделаю свой адаптер глобальным.
Это моя функция заполнения списка
private void populateListView() {
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listdata = new ArrayList<>();
while (data.moveToNext()) {
last_date = expire_Date(data.getString(4));
Calendar c = Calendar.getInstance();
c.setTime(last_date);
c.add(Calendar.DATE, 30);
Date currDate = new Date();
SimpleDateFormat dates = new SimpleDateFormat("M/d/yyyy");
currDate.setTime(System.currentTimeMillis());
int differenceDates;
differenceDates = difOfDay(c, currDate);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
month = month+1;
String date = month + "/" + day + "/" + year;
String first_row =data.getString(3) + " (" + data.getString(1) + ")\n";
String second_row = getResources().getString(R.string.red_info);
listdata.add(first_row + " " + second_row + differenceDates + " (" + date + ")");
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,listdata);
mListView.setAdapter(adapter);
}
И это моя функция удаления:
public AdapterView.OnItemLongClickListener myClickListener = new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,final int position, final long id) {
AlertDialog alert = new AlertDialog.Builder(view.getContext())
.setTitle("Delete ")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mDatabaseHelper.deletechute(position);
Toast.makeText(getApplicationContext(), "Parachute deleted", Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged(); ////Here my code can't resolve my adapter///
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).show();
return false;
}
};