在自定义提醒对话框中使用的ExpandableListView对象为null

时间:2019-04-26 10:05:33

标签: android expandablelistview customdialog

我正在创建一个示例应用程序,在其中单击按钮打开一个自定义警报对话框。包含自定义可扩展列表的自定义对话框。

LangContextWrapper中,我添加了一个按钮。

activity_main.xml

为自定义对话框创建了 <Button android:id="@+id/buttonShowCustomDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Custom Dialog" /> ,并在其中添加了ExpandableListView。

custom.xml

对于ExpandableListView,我还创建了<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/relativeL"> <ExpandableListView android:id="@+id/lvExp" android:layout_height="match_parent" android:layout_width="match_parent"/> </RelativeLayout> list-group.xml

这是我的list-items.xml-

MainActivity.java

在按钮上单击public class MainActivity extends AppCompatActivity { final Context context = this; private Button button; ExpandableListAdapter listAdapter; ExpandableListView expListView; List<String> listDataHeader; HashMap<String, List<String>> listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expListView = (ExpandableListView) findViewById(R.id.lvExp); button = (Button) findViewById(R.id.buttonShowCustomDialog); // add button listeners button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { prepareListData(); AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle("Select something"); listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild); expListView.setAdapter(listAdapter); builder.setView(expListView); AlertDialog dialog = builder.create(); dialog.show(); } }); } private void prepareListData() { listDataHeader = new ArrayList<String>(); listDataChild = new HashMap<String, List<String>>(); // Adding child data listDataHeader.add("Top 250"); listDataHeader.add("Now Showing"); listDataHeader.add("Coming Soon.."); // Adding child data List<String> top250 = new ArrayList<String>(); top250.add("The Shawshank Redemption"); List<String> nowShowing = new ArrayList<String>(); nowShowing.add("The Conjuring"); List<String> comingSoon = new ArrayList<String>(); comingSoon.add("2 Guns"); listDataChild.put(listDataHeader.get(0), top250); // Header, Child data listDataChild.put(listDataHeader.get(1), nowShowing); listDataChild.put(listDataHeader.get(2), comingSoon); } } 总是为空。我无法从自定义对话框中获取expandablelistview对象。

我在这里做错了什么?请提出建议。

这里是expListView

ExpandableListAdapter.java

1 个答案:

答案 0 :(得分:2)

根据您的代码,您将ExpandableListView添加到custom.xml文件中,而不在“活动”中的任何位置使用此文件引用。

此外,您正在从ExpandableListView扩展activity_main.xml并尝试为AlertDialog设置该视图。

如果要在ExpandableListView中显示Dialog,则需要对custom.xml文件进行充气,然后从对话框对象中找到ExpandableListView,然后将适配器与它。

您也可以使用Dialog。检查以下代码。

button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        prepareListData();
        openDialog();
    }
});

下面是openDialog()方法。

public void openDialog() {

    AlertDialog.Builder dialog_custom = new AlertDialog.Builder(getApplicationContext());
    dialog_custom.setTitle("Select something");
    View dialogView = this.getLayoutInflater().inflate(R.layout.custom, null);
    dialog_custom.setView(dialogView);

    ExpandableListView expListView = dialogView.findViewById(R.id.lvExp);
    expListView.setAdapter(new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild));

    AlertDialog alertDialog = dialog_custom.create();
    alertDialog.show();
}
相关问题