在按钮的 OnLongClick 的对话框中显示 ListView

Show ListView in Dialog of OnLongClick of a Button

我正在使用自定义对话框,长按名为 pg 的按钮会调用该对话框。 我想在自定义对话框中显示一个 ListView, ListView 和自定义对话框都有相同的 xml 文件:custome_dialog。 问题是:当我尝试声明 ListView 的适配器时应用程序崩溃。 这是我的 Java Gorups.this 代码:

public class Gorups extends AppCompatActivity {


    Button pg;
    ListView pglist;
    private static String[] NAMES=new String[]{
            "A","B","C"
    }; //Items To show in ListView

    @Override
     protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gorups);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);    


        //LongClick on PG////////////////////////////////////Start
        pg=findViewById(R.id.pgbtn);
        pg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                final AlertDialog.Builder dialog = new AlertDialog.Builder(Gorups.this);
                view = getLayoutInflater().inflate(R.layout.custome_dialog,null);


                dialog.setView(view);
                AlertDialog customDialog = dialog.create();
                customDialog.setTitle("Saved Contacts");
                customDialog.setMessage("Dialog is Shown");

                //Declaring ListView Adapter
                    pglist = (ListView) findViewById(R.id.lvpg);
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(Gorups.this, R.layout.custome_dialog, NAMES);
                    pglist.setAdapter(adapter);




                //Calling ListView:--//////////////////////////////////////////////////

                pglist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        String value=(String)pglist.getItemAtPosition(position);
                        Toast.makeText(Gorups.this,"U pressed: "+position+" and: "+value,Toast.LENGTH_SHORT).show();
                    }
                });
                customDialog.show();

                return true;
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }

这里是custome_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="25dp">


    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ListView
            android:id="@+id/lvpg"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

它有什么问题吗?

使用这个

pglist = (ListView) dialog.findViewById(R.id.lvpg);

而不是这个

pglist = (ListView) findViewById(R.id.lvpg);

Another issue is you are setting same layout to your listview adapter custome_dialog check it

试试这个

 ArrayAdapter<String> adapter = new ArrayAdapter<>(Gorups.this, android.R.layout.simple_list_item_1, NAMES);

OR TRy this

Dialog dialog=new Dialog(Gorups.this);
dialog.setTitle("Saved Contacts");     
dialog.setContentView(R.layout.custome_dialog);     
pglist = (ListView) dialog.findViewById(R.id.lvpg);;    


 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
        Gorups.this,
        android.R.layout.simple_list_item_1, NAMES );
pglist.setAdapter(arrayAdapter);
dialog.show();

编辑

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ListView
        android:id="@+id/lvpg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

代替这一行:

view = getLayoutInflater().inflate(R.layout.custome_dialog,null);

使用:

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.custome_dialog,null);

同时使用:

pglist = (ListView) dialog.findViewById(R.id.lvpg);

而不是:

pglist = (ListView) findViewById(R.id.lvpg);

并将所有Groups.this替换为getContext()

并替换为:

new ArrayAdapter<>(Gorups.this, R.layout.custome_dialog, NAMES);

与:

new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1, NAMES);