代码之家  ›  专栏  ›  技术社区  ›  Zubair Younas

在仅长按按钮的对话框中显示ListView

  •  0
  • Zubair Younas  · 技术社区  · 7 年前

    我使用的是一个自定义对话框,在长时间点击一个名为pg的按钮时调用。 我想在自定义对话框中显示ListView, ListView和Custom Dialog都有相同的xml文件:custome\u Dialog . 问题是:当我试图声明ListView的适配器时,应用程序崩溃。 这是我的Java代码 戈鲁普斯。这 :

    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_对话框。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>
    

    这有什么问题?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Goku Farhana Naaz Ansari    7 年前

    使用此

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

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

    另一个问题是,您正在将相同的布局设置为 listview 适配器 自定义对话框 检查一下

    试试这个

     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>
    

        2
  •  1
  •   Muhammad Saad Rafique    7 年前

    而不是这一行:

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

    并替换所有组。这与getContext()

    并将其替换为:

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

    使用:

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