前提・実現したいこと
Android Studioでtodoアプリのようなものを作っています。
MainActivityにリスト一覧が表示されていて、リストの要素を長押しした時に、削除の確認ダイアログが出て、OKがクリックされた時に削除する流れを作ったのですが、削除した後リスト一覧を更新したいのですが、それができず困っています。
削除した後リスト一覧を更新する方法を教えていただきたいです。
該当のソースコード
コードは質問に関連する部分だけを抜粋しています。
MainActivity:
Java
1public class MainActivity extends AppCompatActivity { 2 @Override 3 protected void onResume() { 4 super.onResume(); 5 6 selectDb(); 7 } 8 public void selectDb() { 9 _helper = new DatabaseHelper(MainActivity.this); 10 11 SQLiteDatabase db = _helper.getWritableDatabase(); 12 13 String[] projection = { 14 DatabaseContract._ID, 15 DatabaseContract.COLUMN_NAME_TITLE, 16 }; 17 18 _cursor = db.query( 19 DatabaseContract.TABLE_NAME, 20 projection, 21 null, 22 null, 23 null, 24 null, 25 null 26 ); 27 28 AACursorAdapter _adapter = new AACursorAdapter(MainActivity.this, _cursor); 29 30 ListView lvMemo = findViewById(R.id.lvMemo); 31 32 lvMemo.setAdapter(_adapter); 33 34 lvMemo.setOnItemClickListener(new ListItemClickListener()); 35 36 lvMemo.setOnItemLongClickListener(new ListItemLongClickListener()); 37 } 38 39 private class ListItemClickListener implements AdapterView.OnItemClickListener{ 40 @Override 41 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 42 } 43 } 44 45 private class ListItemLongClickListener implements AdapterView.OnItemLongClickListener { 46 @Override 47 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 48 DeleteConfirmDialogFragment dialogFragment = new DeleteConfirmDialogFragment(); 49 50 _cursor = (Cursor) parent.getItemAtPosition(position); 51 int memoId = _cursor.getInt(0); 52 String memoTitle = _cursor.getString(1); 53 54 int main = -3; 55 56 Bundle args = new Bundle(); 57 args.putString("memoTitle", memoTitle); 58 args.putInt("memoId", memoId); 59 60 args.putInt("activity", main); 61 62 dialogFragment.setArguments(args); 63 64 dialogFragment.show(getSupportFragmentManager(), "DeleteConfirmDialogFragment"); 65 return true; 66 } 67 } 68}
AACursorAdapter:
Java
1public class AACursorAdapter extends CursorAdapter { 2 3 private LayoutInflater mInflater; 4 5 public AACursorAdapter(Context context, Cursor cursor) { 6 super(context, cursor); 7 8 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 9 } 10 11 static class ViewHolder { 12 TextView title; 13 } 14 15 @Override 16 public View newView(Context context, Cursor cursor, ViewGroup viewGroup) { 17 View view = mInflater.inflate(R.layout.memo_list_item, null); 18 19 ViewHolder holder = new ViewHolder(); 20 holder.title = (TextView) view.findViewById(R.id.tvTitle); 21 22 view.setTag(holder); 23 24 return view; 25 } 26 27 @Override 28 public void bindView(View view, Context context, Cursor cursor) { 29 ViewHolder holder = (ViewHolder) view.getTag(); 30 31 int memoId = cursor.getInt(0); 32 String memoTitle = cursor.getString(1); 33 34 holder.title.setText(memoTitle); 35 } 36 37}
DeleteConfirmDialogFragment:
Java
1public class DeleteConfirmDialogFragment extends DialogFragment { 2 3 String _memoTitle; 4 int _memoId; 5 int _activity; 6 7 Activity _parentActivity; 8 9 @Override 10 public Dialog onCreateDialog(Bundle savedInstanceState){ 11 _memoTitle = getArguments().getString("memoTitle", ""); 12 _memoId = getArguments().getInt("memoId", -1); 13 14 _activity = getArguments().getInt("activity", 0); 15 16 17 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 18 19 String message = _memoTitle + getString(R.string.dialog_msg_delete); 20 21 builder.setTitle(R.string.dialog_title_delete); 22 builder.setMessage(message); 23 builder.setPositiveButton(R.string.dialog_btn_ok, new DialogButtonClickListener()); 24 builder.setNegativeButton(R.string.dialog_btn_ng, new DialogButtonClickListener()); 25 26 AlertDialog dialog = builder.create(); 27 return dialog; 28 } 29 30 private class DialogButtonClickListener implements DialogInterface.OnClickListener { 31 @Override 32 public void onClick(DialogInterface dialog, int which) { 33 String msg = ""; 34 35 switch (which){ 36 case DialogInterface.BUTTON_POSITIVE: 37 delete(); 38 msg = _memoTitle + getString(R.string.dialog_ok_delete_toast); 39 40 if (_activity == -5){ 41 _parentActivity.finish(); 42 } else if(_activity == -3) { 43 //_parentActivity. 44 } 45 46 break; 47 case DialogInterface.BUTTON_NEGATIVE: 48 msg = getString(R.string.dialog_ng_delete_toast); 49 break; 50 } 51 52 Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show(); 53 } 54 } 55 56 public void delete() { 57 _parentActivity = getActivity(); 58 59 DatabaseHelper helper = new DatabaseHelper(_parentActivity); 60 61 try (SQLiteDatabase db = helper.getWritableDatabase()){ 62 db.delete(DatabaseContract.TABLE_NAME, 63 DatabaseContract._ID + " = ? ", 64 new String[] {String.valueOf(_memoId)}); 65 } 66 } 67}
回答2件
あなたの回答
tips
プレビュー