Android 如何使用recyclerview成功创建对话片段?

Android 如何使用recyclerview成功创建对话片段?,android,android-recyclerview,android-alertdialog,android-dialogfragment,Android,Android Recyclerview,Android Alertdialog,Android Dialogfragment,'这是我的适配器类代码,当我长按某个项时,它会显示一个删除该项的警告对话框。.我想从循环视图调用DialogFragment。如何从Recyclerview适配器解析此show方法。怎么可能呢 适配器类 它将错误显示为….java.lang.ClassCastException:android.app.Application无法强制转换为androidx.appcompat.app.AppCompatActivity 在com.example.recyclerview1.myadapter$1.

'这是我的适配器类代码,当我长按某个项时,它会显示一个删除该项的警告对话框。.我想从循环视图调用DialogFragment。如何从Recyclerview适配器解析此show方法。怎么可能呢

适配器类

它将错误显示为….java.lang.ClassCastException:android.app.Application无法强制转换为androidx.appcompat.app.AppCompatActivity 在com.example.recyclerview1.myadapter$1.onLongClick(myadapter.java:49)

MainActivity.java

package com.example.recyclererview1;
导入androidx.appcompat.app.appcompat活动;
导入androidx.recyclerview.widget.LinearLayoutManager;
导入androidx.recyclerview.widget.recyclerview;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
公共类MainActivity扩展了AppCompatActivity{
ArrayList=list=new ArrayList();
回收站;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rc=findViewById(R.id.myrec);
学生s1=新生(“艾哈迈德”,“34198723482”);
学生s2=新生(“艾哈迈德”,“34198723482”);
学生s3=新生(“艾哈迈德”,“34198723482”);
学生s4=新生(“艾哈迈德”,“34198723482”);
学生s5=新生(“艾哈迈德”,“34198723482”);
学生s6=新生(“艾哈迈德”,“34198723482”);
学生s7=新生(“艾哈迈德”,“34198723482”);
学生s8=新学生(“艾哈迈德”,“34198723482”);
学生s9=新生(“艾哈迈德”,“34198723482”);
学生s0=新学生(“艾哈迈德”,“34198723482”);
列表。添加(s1);
列表。添加(s2);
增加(s3);
列表。添加(s4);
列表。添加(s5);
附表.增补(中六);
列表。添加(s7);
列表。添加(s8);
列表。添加(s9);
列表。添加(s0);
MyAdapter MyAdapter=新的MyAdapter(getApplicationContext(),list);
LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(此);
rc.setLayoutManager(线性布局管理器);
rc.setAdapter(myadapter);
}
}
下一行的问题

dialog.show(((AppCompatActivity)c).getSupportFragmentManager()
c是上下文,因此不能强制转换为AppCombatActivity

尝试在
myadapter
类中发送活动对象,如下所示

public MyAdaptor (Activity ctx,ArrayList<Student> list)

您可以创建自定义对话框

如果没有任何布局,请在XML文件布局中添加
RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="300dp">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</FrameLayout>

我希望这对您有所帮助。

我认为您正在MyAdapter构造函数中传递应用程序上下文。你能在“活动”中为初始化适配器的位置添加代码吗?好的,我添加了代码。检查它。更改此行
myadapter myadapter=new myadapter(MainActivity.this,list)
;还有这个
dialog.show(((MainActivity)c).getSupportFragmentManager(),“Fragment”)为什么要在RecycleService中创建DialogFragment我已经在MyAdapter类中发送了这样的活动对象…因为您的错误表明您正在Adapter类中发送android.app.Application对象。在MainActivity类中,您需要像下面这样传递-MyAdapter MyAdapter=new MyAdapter(MainActivity.this,list);并将适配器构造函数更新为以下-myadapter(活动ctx,ArrayList列表)
dialog.show(((AppCompatActivity)c).getSupportFragmentManager()
public MyAdaptor (Activity ctx,ArrayList<Student> list)
MyAdaptor myAdaptor = new MyAdaptor(MainActivity.this,list);
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="300dp">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</FrameLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //TODO Change 'frag_mdialog' to your custom view XML layout 
    View v = inflater.inflate(R.layout.frag_mdialog, container, false);
    view = v;

    // Do all the stuff to initialize your custom view

    RecyclerView recyclerView = view.findViewById(R.id.rv_dialog);

    return v;
}