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

索纳利

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

在 中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始终为空。我无法从自定义对话框中获取 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;
    }
}
阿杰·梅塔

根据您的代码,您正在添加您ExpandableListViewcustom.xml文件,而不是在您的活动中的任何地方使用此文件引用。

此外,您正在膨胀您的ExpandableListViewfromactivity_main.xml并尝试为AlertDialog.

如果你想ExpandableListView在你的里面显示Dialog,那么你需要膨胀你的custom.xml文件,然后ExpandableListView从你的对话框对象中找到,然后用它绑定适配器。

您也可以使用对话框。检查下面的代码。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中使用网格视图创建自定义警报对话框?

ExpandableListView getChildAt()返回null

Android警报对话框的自定义面板和按钮面板的高度为0

自定义对话框的侦听器null

如何关闭“自定义警报”对话框

Android中的“自定义警报”对话框

如何自定义警报对话框,以使按钮适合警报对话框

使用new后对象为NULL

在.NET中使用后将对象设置为Null / Nothing

ExpandableListView 在片段中为空白

Mockito对象为null

IConfiguration对象为null。

Modelmapper:当源对象为null时,如何应用自定义映射?

自定义分页列表将 IQueryable 中的子对象设置为 null

Entity Framework 中的自定义 SQL,结果为 null 的对象

android expandablelistview 使用 volley

自定义Android对话框中的EditText返回null

尽管使用usercontrol checkboxlist检查是否为null,但返回的对象引用未设置为对象的实例

Redy状态对象为简单Ajax的null或未定义

制作圆形自定义警报对话框:着色区域不正确

Flutter-自定义警报对话框未显示

在服务android中显示自定义警报对话框

Android-如何在自定义警报对话框中检查按钮单击?

通过android自定义警报对话框传递额外数据

具有RecyclerView的自定义警报对话框

自定义警报对话框不会关闭

如何在自定义警报对话框中刷新内容/ UI

具有多个EditText的自定义警报对话框

两个活动的一个自定义警报对话框