0

I have 2 Custom ListViews. I use one to load all data from database with an Edit and Delete Button. I use the other one as like a message center instead of toast so when data is added to the database it will populate with "Variable was added to Database" or "Variable Was Not Added To Database"

All of this works fine. Now when you click on View All, it loads all the data from the database and puts an EDIT button and a DELETE button. When the user clicks on Delete, it pops up a YES NO dialogue and Asks if they are sure they want to delete. If the press no, just close the dialogue, if they answer yes Delete it from the database. It them loads the listview back up with all the data minus the deleted item. I am using Toast currently but want to replace it.

When the user clicks on YES to delete what I want it to do is the following:

  1. Delete it from the database. (Which it does.)
  2. Store and Pass the delete information to the other ListView and populate the listview with the message "VARIABLE DATA Was Deleted" or "Variable DATA was NOT DELETED".

I am not quite sure how to reload the activity with the other Listview and or how to pass data from the custom adapter Yes No Dialogue to it.

I do believe I just have to use INTENT with put extra, and somehow rewrite the original activity with get extra??

Just haven't found anything quite what I am looking for to do what I need done.

Just with all my other questions I don't really want the answer in code, I would like to know what direction I need to go in and any knowledge on doing what I need to do and possible examples and or resources that may have examples and an explanation.

Again all code is working, just not sure where to start with grabbing the needed data from the Yes No Dialogue from the custom adapter and how to pass to the activity with a different listview.

In my activity I do the following using customadapter to load listview with Edit and Delete Buttons:

CustomCategoriesAdapter adapter = new CustomCategoriesAdapter(CategoriesActivity.this,
                              R.layout.lv_all_category,
                              AllCategory);
listView.setAdapter(adapter);

In my CustomCategoriesAdapter I use a delete button that calls my removeCategory function passing in the position and the ID that is needed to be passed to sqlite to delete the row.

removeCategory(position, DeletedCategoryId);

In my removeCategory I have the AlertDialogue with Yes and No and I have set the Possitive and Negative Buttons.

In my Positive Button is the following (remember this is in the CustomCategoriesAdapter)

public void onClick(DialogInterface dialogInterface, int i) {
            DataBaseHelper dataBaseHelper = new DataBaseHelper(getContext());
            boolean deletedSuccessfully = dataBaseHelper.delete_Categories(DeletedCategoryId);

            if(deletedSuccessfully == true){
                Categories.remove(position);
                notifyDataSetChanged();
                Toast toast = Toast.makeText(getContext(), DeletedCategoryId + " Was Deleted",Toast.LENGTH_SHORT);
                toast.show();
            }else{
                Toast toast = Toast.makeText(getContext(), DeletedCategoryId + " Was NOT Deleted",Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    });

What I want to do is replace everything in the deletedSuccessfully if else statement and replace it with the code that will RELOAD the customCategoriesAdapter with a different listview file while passing the DeletedCategoryId to it and populating the ListView with the message that it was or was not deleted instead of using toast.

Is it possible to call CustomCategoriesAdapter again from within CustomCategoriesAdapter with the different ListView like below:

    CustomCategoriesAdapter adapter = new CustomCategoriesAdapter(CategoriesActivity.this,
                              R.layout.lv_message,
                              DeletedMessage);
listView.setAdapter(adapter);

I know that I would not beable to use CategoriesActivity.this, that most likely it would have to be getActivity() or something like this. But is this works how would I go about passing in the data / message and adding it to the listview?

Here is an update to new code, it doesn't show any errors but the app crashes.

From setPositive button I call a function called getMessageListView passing DeletedCategoryName and view:

getMessageListView(DeletedCategoryName, view);

Now I had to define listview and find it, along with my objects. Here is the code that keeps crashing on me and I am not sure why?

private void getMessageListView(String DeletedCategoryName, View view){
ListView listView;
List<IncomeCategoriesClass> DeletedIncomeCategory = new LinkedList<>();
IncomeCategoriesClass DeleteIncomeCategory;
DeleteIncomeCategory = new IncomeCategoriesClass();
DeleteIncomeCategory.setCATEGORY_NAME(DeletedCategoryName + " Was Deleted From Database.");
DeletedIncomeCategory.add(DeleteIncomeCategory);
CustomIncomeCategoriesAdapter adapter = new CustomIncomeCategoriesAdapter(getContext(),R.layout.lv_add_income_category, DeletedIncomeCategory);
listView = view.findViewById(R.id.lvIncomeCategories);
listView.setAdapter(adapter);

}

The crash report is as follows:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.accounting, PID: 3839
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.example.accounting.CustomIncomeCategoriesAdapter.getMessageListView(CustomIncomeCategoriesAdapter.java:110)
    at com.example.accounting.CustomIncomeCategoriesAdapter.access$100(CustomIncomeCategoriesAdapter.java:28)
    at com.example.accounting.CustomIncomeCategoriesAdapter$2.onClick(CustomIncomeCategoriesAdapter.java:83)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I/Process: Sending signal. PID: 3839 SIG: 9 Process 3839 terminated.

I know something is NULL

Thank You.

Shawn Mulligan
  • 103
  • 1
  • 8
  • Use **interface**, then you can pass data from adapter back to fragment/activity and update the other adapter. https://stackoverflow.com/questions/61605931/how-to-manage-multiple-positions-in-recyclerview-onclicklistener/61627290#61627290 and https://stackoverflow.com/questions/67302993/adding-toast-to-every-childitem-of-a-multi-level-expandable-lisview/67420387#67420387 may help. – i_A_mok May 29 '21 at 07:35
  • okay the issue is that I will be using the same adapter, just with a different layout file. – Shawn Mulligan May 29 '21 at 18:22

1 Answers1

0

The solution was easier than what I thought it was going to be:

CustomAdapter.java

Intent intent = new Intent(context, CategoriesActivity.class);
intent.putExtra("message", DeletedCategoryName);
context.startActivity(intent);

In the CategoriesActivity.java that called the CustomAdapter.java to begin with:

Intent intent = getIntent();
    Bundle myextras = intent.getExtras();
    if(myextras!=null){
        String mymessage = (String)myextras.get("message");
        postDeleteMessage(mymessage);

And then to top it off postDeleteMessage passes mymessage back to the customadapter to show in the custom listview layout that I need.

Shawn Mulligan
  • 103
  • 1
  • 8