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



我正在使用一个自定义对话框,该对话框被称为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);

另一个问题是,您将相同的布局设置为listview适配器 CUSTOME_DIALOG 检查它

尝试这个

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

或尝试此

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

并替换所有组。

并替换:

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

with:

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

最新更新