Home > Back-end >  Listview Unable to update when inserting a new data from custom dialog
Listview Unable to update when inserting a new data from custom dialog

Time:12-31

I'm trying to display data from SQLite database to a custom listview. The data is inserted from a custom dialog and the user click on the positive button it should update the listview, but the listview doesn't update. It only update when I click the back button and reenter the Activity. what should I do?

SubjectListActivity.java

public class SubjectListActivity extends AppCompatActivity {
    private ListView lv_SubjectList;
    private FloatingActionButton btn_subjectAdd, btn_subjectView;
    DatabaseHelper databaseHelper;
    ArrayList<SubjectListModel> arrayList;
    SubjectListAdapter subjectListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_list);
        intViews();
        databaseHelper = new DatabaseHelper(this);
        arrayList = new ArrayList<>();
        loadListView();

        btn_subjectAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDialog();
                loadListView();
            }
        });

        btn_subjectView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    private void loadListView () {
        arrayList = databaseHelper.getAllSubjectListData();
        subjectListAdapter = new SubjectListAdapter(this, arrayList);
        lv_SubjectList.setAdapter(subjectListAdapter);
    }

    public void openDialog () {
        SubjectListDialog subjectListDialog = new SubjectListDialog();
        subjectListDialog.show(getSupportFragmentManager(), "subject list dialog");
    }

    private void intViews() {
        lv_SubjectList = findViewById(R.id.lv_SubjectList);
        btn_subjectView = findViewById(R.id.btn_subjectDelete);
        btn_subjectAdd = findViewById(R.id.btn_subjectAdd);
    }
}

SubjectListDialog.java

public class SubjectListDialog extends AppCompatDialogFragment {
    private EditText et_subjectCode, et_subjectName, et_creditHour;
    DatabaseHelper databaseHelper;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        databaseHelper = new DatabaseHelper(getContext());
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.subject_list_dialog_layout, null);

        builder.setView(view)
                .setTitle("Add Subject")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setPositiveButton("confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        insert();
                    }
                });
        et_subjectCode = view.findViewById(R.id.et_subjectCode);
        et_subjectName = view.findViewById(R.id.et_subjectName);
        et_creditHour = view.findViewById(R.id.et_creditHour);
        return builder.create();
    }

    private void insert() {
        boolean result = databaseHelper.insertSubjectList(et_subjectCode.getText().toString(), et_subjectName.getText().toString(),Integer.parseInt(et_creditHour.getText().toString()));
        if (result) {
            Toast.makeText(requireContext(), "Data has been added", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "Please insert all the fields correctly", Toast.LENGTH_SHORT).show();
        }
    }

}

CodePudding user response:

Seemingly, you don't reload the data after user click on the positive button. You can override onResume method and add more code to update the data in the list and call adapter.notifyDataSetChanged() to refresh the data in listview.

CodePudding user response:

Put this code into your onResume()

databaseHelper = new DatabaseHelper(this);
arrayList = new ArrayList<>();
loadListView();

Actually there are many ways to get refresh data in recyclerview. But in my experience i always do this because easy to use, and ofc work. you can try to understanding the lifecycle of android by this https://developer.android.com/guide/components/activities/activity-lifecycle

  • Related