在自定义警报对话框中使用时,ExpandableListView 对象为 null
ExpandableListView object null when used inside custom alert dialog
我正在创建一个示例应用程序,我在其中单击一个按钮打开一个自定义警报对话框。包含自定义可扩展列表的自定义对话框。
在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.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);
}
}
单击按钮时 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;
}
}
根据您的代码,您将 ExpandableListView
添加到 custom.xml
文件中,而不是在 Activity.
中的任何地方使用此文件引用
您还从 activity_main.xml
膨胀 ExpandableListView
并尝试为 AlertDialog
设置视图。
如果你想在你的 Dialog
中显示 ExpandableListView
,那么你需要扩充你的 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();
}
我正在创建一个示例应用程序,我在其中单击一个按钮打开一个自定义警报对话框。包含自定义可扩展列表的自定义对话框。
在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.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);
}
}
单击按钮时 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;
}
}
根据您的代码,您将 ExpandableListView
添加到 custom.xml
文件中,而不是在 Activity.
您还从 activity_main.xml
膨胀 ExpandableListView
并尝试为 AlertDialog
设置视图。
如果你想在你的 Dialog
中显示 ExpandableListView
,那么你需要扩充你的 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();
}