Search code examples
androidlistviewandroid-arrayadaptercustomdialog

Show ListView in Dialog of OnLongClick of a Button


I'm using a Custom Dialog, which is called on Long Click of a button named pg. I want to Show a ListView in the Custom Dialog, Both ListView and Custom Dialog have same xml file: custome_dialog. The Problem is: App crashes when I try to declare the Adapter of ListView. Here is my Java Code of 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);
    }

Here is 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>

What could be wrong with it?


Solution

  • use this

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

    instead of this

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

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

    try this

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

    EDIT

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