在自定义警报对话框中使用时,ExpandableListView对象为null

问题描述 投票:0回答:1

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

activity_main.xml,我添加了一个按钮。

 <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />

为自定义对话框创建了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>

对于ExpandableListView,我还创建了list-group.xmllist-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);
    }
}

在按钮上单击expListView始终为null。我无法从自定义对话框中获取expandablelistview对象。

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

这是ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
android expandablelistview customdialog
1个回答
2
投票

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

你也是从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();
}
© www.soinside.com 2019 - 2024. All rights reserved.