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 ..

    1 answer 1

    listAdapter is invisible in the method. Everything will be fine if the listAdapter is declared static in the class or if it is passed to the deleteChoose () method as a parameter.

     public void deleteChoose (CursorAdapter cursorAdapter){ long[] cip = listView.getCheckedItemIds(); for (int i = 0; i < cip.length; i++) { long aCip = cip[i]; db.delete(TABLE_NAME, COLUMN_ID + "=" + aCip, null); } cursorAdapter.notifyDataSetChanged (); } 
    • static allowed the cursor to make the adapter visible, but the nullpointerexception still remained (( - ZigZag
    • Try the second option - pass listAdapter as a parameter. If you are interested in working with a static instance, then you must make sure that you do not specify the class (type) of the listAdapter instance in the CursorAdapter listAdapter = new SimpleCursorAdapter (this, ...). I note that the option of passing the parameter is still preferable in this case, in my opinion. - DimXenon
    • Let me remind you that static fields are available at the class level, which means that if there are many instances of this class, you cannot guarantee that the necessary object is under the listAdapter. - DimXenon
    • I tried it, but it is even worse there .. this method is called from the dialog fragment through the interface and begins to swear that the method in the interface has no parameters .. and it seems to me not quite correct to draw parameters into the dialog - ZigZag
    • Yes. Interfaces do not need to break. So static. And how is the listView used in the deleteChoose method declared? Simple class field or static too? - DimXenon