In the onCreate method, I populate the listview from the database and hang listeners on the listview elements.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_groups_delete); SQLiteOpenHelper openHelper = new DataBaseOpenHelper(this); db = openHelper.getWritableDatabase() ; //открытие БД на запись //связь ListView с данными из БД listView = (ListView)findViewById(R.id.nameDelGroup); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//устанавливаем режим выбора cursor = db.query("groups", new String[] {"_id", "name_group"},null, null, null, null, "name_group ASC"); CursorAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cursor, new String[]{"name_group"}, new int[] {android.R.id.text1},0 ); listView.setAdapter(listAdapter); // слушатель элементов ListView listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { scanSelected(); } }); } delete items from database using deleteChoose method:
public void deleteChoose (){ long[] cip = listView.getCheckedItemIds(); for (int i = 0; i < cip.length; i++) { long aCip = cip[i]; db.delete(TABLE_NAME, COLUMN_ID + "=" + aCip, null); } listAdapter.notifyDataSetChanged (); } but when calling listAdapter.notifyDataSetChanged (); I get nullpointerexception, probably due to the lack of an instance of the object, but I can not understand why?, he seemed to stay ..