【问题标题】:How to load activity from custom adapter Yes No Dialogue passing data to a listview?如何从自定义适配器加载活动 Yes No 对话将数据传递到列表视图?
【发布时间】:2021-08-16 08:28:54
【问题描述】:

我有 2 个自定义列表视图。我使用一个通过编辑和删除按钮从数据库中加载所有数据。我将另一个用作消息中心而不是 toast,因此当将数据添加到数据库时,它将填充“变量已添加到数据库”或“变量未添加到数据库”

所有这些都可以正常工作。现在,当您单击查看全部时,它会从数据库中加载所有数据并放置一个 EDIT 按钮和一个 DELETE 按钮。当用户点击 Delete 时,它​​会弹出一个 YES NO 对话框并询问他们是否确定要删除。如果按否,则关闭对话,如果他们回答是,则将其从数据库中删除。他们加载列表视图备份所有数据减去已删除的项目。我目前正在使用 Toast,但想更换它。

当用户点击“是”删除我想要它做的事情如下:

  1. 从数据库中删除它。 (确实如此。)
  2. 存储删除信息并将其传递给另一个 ListView,并使用消息“变量数据已删除”或“变量数据未删除”填充列表视图。

我不太确定如何使用其他 Listview 重新加载 Activity,以及如何将数据从自定义适配器 Yes No Dialogue 传递给它。

我确实相信我只需要使用带有 put extra 的 INTENT,并以某种方式使用 get extra 重写原始活动??

只是还没有找到我正在寻找的东西来做我需要做的事情。

就我的所有其他问题而言,我真的不想要代码中的答案,我想知道我需要朝哪个方向发展,以及关于做我需要做的事情的任何知识以及可能的示例和或资源有例子和解释。

再次所有代码都在工作,只是不确定从哪里开始从自定义适配器的 Yes No Dialogue 中获取所需的数据,以及如何使用不同的列表视图传递给 Activity。

在我的活动中,我使用 customadapter 执行以下操作,以使用编辑和删除按钮加载列表视图:

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

在我的 CustomCategoriesAdapter 中,我使用了一个删除按钮,该按钮调用我的 removeCategory 函数,传入位置和需要传递给 sqlite 以删除行的 ID。

removeCategory(position, DeletedCategoryId);

在我的 removeCategory 中,AlertDialogue 带有 Yes 和 No,并且我设置了正负按钮。

在我的正面按钮中如下(记住这是在 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();
            }
        }
    });

我想要做的是替换 deletedSuccessfully if else 语句中的所有内容,并将其替换为将使用不同的 listview 文件重新加载 customCategoriesAdapter 的代码,同时将 DeletedCategoryId 传递给它并使用它是的消息填充 ListView 或没有被删除,而是使用 toast。

是否可以在 CustomCategoriesAdapter 中使用不同的 ListView 再次调用 CustomCategoriesAdapter,如下所示:

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

我知道我不能使用 CategoriesActivity.this,很可能它必须是 getActivity() 或类似的东西。但这可行吗?我将如何传递数据/消息并将其添加到列表视图中?

这是对新代码的更新,它没有显示任何错误,但应用程序崩溃了。

从 setPositive 按钮我调用一个名为 getMessageListView 的函数,传递 DeletedCategoryName 和视图:

getMessageListView(DeletedCategoryName, view);

现在我必须定义 listview 并找到它以及我的对象。这是一直在我身上崩溃的代码,我不知道为什么?

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);

}

崩溃报告如下:

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:发送信号。 PID:3839 SIG:9 进程 3839 终止。

我知道有些东西是 NULL

谢谢。

【问题讨论】:

标签: android android-studio android-listview


【解决方案1】:

解决方案比我想象的要容易:

CustomAdapter.java

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

在以调用 CustomAdapter.java 开头的 CategoriesActivity.java 中:

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

然后最重要的是 postDeleteMessage 将 mymessage 传递回 customadapter 以显示在我需要的自定义列表视图布局中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2018-08-14
    • 2019-11-11
    • 2015-08-30
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多