setAdapter 不适用于对话框内的列表

setAdapter doesn't work for a list inside a dialog

我正在使用此布局呈现对话框

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
style="ThemeDialog"
android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    >

<CheckBox
    android:id="@+id/AddToDictionary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="fill"
    android:checked="false"
    android:text="Add" />
<Button
    android:id="@+id/declineButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Confirm" />
    <Button
        android:id="@+id/eraseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Erase" />
</LinearLayout>


<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/border" />
 </LinearLayout>

这是我的方法中的代码,用于在普通 activity 中显示项目列表,效果非常好,但我尝试在添加到对话框的列表中显示相同的列表,但这并没有虽然调试器显示引用 dialog_suggestions_list_view 不为空,但 array(我的 arrayAdapter)也不为空,我需要你的帮助指出这里的问题在哪里,为什么 setAdapter 不适用于对话框中的列表?

   dialog = new Dialog(MainActivity.this);
                    // Include dialog.xml file
                    dialog.setContentView(R.layout.dialog);


                    LayoutInflater inflater = (LayoutInflater)   this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(R.layout.dialog, null);
                    dialog_suggestions_list_view=(ListView) view.findViewById(R.id.listView2) ;

                    if(dialog_suggestions_list_view!=null){
                        dialog_suggestions_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
                                String selectedFromList = (String)(dialog_suggestions_list_view.getItemAtPosition(position));
                                AppCompatEditText fContent = (AppCompatEditText) findViewById(R.id.et_content);
                                fContent.append(selectedFromList);
                                fContent.append(" ");
                            }
                        });
                    }else{
                        Log.i("test","my list not found");
                    }

  ArrayList<String> array=new ArrayList<String>();
    array.add("Tic");
    array.add("Tac");
    array.add("Toe");
    
    ArrayAdapter < String > arrayAdapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, array);

    //display listView in ordianr activity
    listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(arrayAdapter);

    //display listView in dialog
    if(dialog_suggestions_list_view!=null){
        dialog_suggestions_list_view.setAdapter(arrayAdapter);
    }else{
        Log.i("test","my list not found");
    }

我对你的代码很困惑,因为它缺少一些部分,但我这样做了,看看是否有帮助。

    dialog = new Dialog(MainActivity.this);
    View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog), null);
    
    dialog.setContentView(view);
    dialog_suggestions_list_view = (ListView) view.findViewById(R.id.listView2);
    dialog_suggestions_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
            try {
                String selectedFromList = (String)(dialog_suggestions_list_view.getItemAtPosition(position));
                AppCompatEditText fContent = (AppCompatEditText) findViewById(R.id.et_content);
                fContent.append(selectedFromList);
                fContent.append(" ");
            } catch (Exception e) {
                Log.i("test","my list not found");
            }
                
        }
    });
}

public ArrayList<String> getList(){
    ArrayList<String> array = new ArrayList<String>();
    array.add("Tic");
    array.add("Tac");
    array.add("Toe");
    return array;
}
  
public void setInitialContentValue(){
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getList());

    //display listView in ordianr activity
    listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(arrayAdapter);

    //display listView in dialog
    try {
        dialog_suggestions_list_view.setAdapter(arrayAdapter);
    } catch (Exception e) {
        Log.i("test","my list not found");
    }
}