单击 ExpandableListview 中的项目时应用程序停止 Android
Application stops when clicked on item in ExpandableListview Android
我正在尝试创建一个 ExpandableListView,下面是我为可扩展列表创建数据的代码
DBHelper dbHelper;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
HashMap<String, List<String>> hashDataChild;
ArrayList<DetailsOfLocation> arrayList;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
expListView = (ExpandableListView) v.findViewById(R.id.lvExp); // get the listview
initializeDatabase();
prepareChildData();
listAdapter = new ExpandListAdapter(getActivity(), arrayList, hashDataChild);
expListView.setAdapter(listAdapter);
return v;
}
public void initializeDatabase()
{
dbHelper = new DBHelper(getActivity());
try {
dbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
private void prepareChildData(){
hashDataChild = new HashMap<String, List<String>>();
//arrayList has a multiple column in it where it will be displayed as the header. The data is from the database
Integer id = ((MyApplication) getActivity().getApplication()).getID();
arrayList = new ArrayList<DetailsOfLocation>();
arrayList = dbHelper.getList(id);
//child is the child's data
List<String> child = new ArrayList<String>();
child.add("Other Info");
child.add("Picture");
child.add("Contact");
//the header and child is being inserted
for(DetailsOfLocation list : arrayList){
hashDataChild.put(String.valueOf(list), child);
}
}
}
当我调试它时,hashDataChild 具有我期望的值,其中每个 header 有 3 个 child。但是在我的 phone 上尝试时它崩溃了并且在 logcat 中没有显示任何内容。我用于创建可扩展列表视图的参考是 [this]:http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
我在想可能是因为ExpandableListview OnGroupClickListener 没有工作?我该如何解决这个问题?它进入我的 onGroupClick 但不显示 child
的列表
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(
getActivity().getApplicationContext(),
arrayList.get(groupPosition)
+ " : "
+ hashDataChild.get(
arrayList.get(groupPosition)), Toast.LENGTH_SHORT)
.show();
return false;
}
});
下面是我的可扩展适配器代码
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<DetailsOfLocation> mDetailsWithNoLocation;
private HashMap<String, List<String>> mchildOfDetailsWithNoLocation;
public ExpandListAdapter(Context context, ArrayList<DetailsOfLocation> detailsWithNoLocation, HashMap<String, List<String>> HashChildData) {
this._context = context;
this.mDetailsWithNoLocation = detailsWithNoLocation; //header
this.mchildOfDetailsWithNoLocation = HashChildData; //hash data
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.mchildOfDetailsWithNoLocation.get(this.mDetailsWithNoLocation.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.expandlist_child_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.tvother_info);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.mchildOfDetailsWithNoLocation.get(this.mDetailsWithNoLocation.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.mDetailsWithNoLocation.get(groupPosition);
}
@Override
public int getGroupCount() {
return mDetailsWithNoLocation.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandlist_group_item, null);
}
TextView lblListDesc = (TextView) convertView.findViewById(R.id.tv_desc);
TextView lblListRoom = (TextView) convertView.findViewById(R.id.tv_location);
TextView lblListAudience = (TextView) convertView.findViewById(R.id.tv_audience);
TextView lblListSpeaker = (TextView) convertView.findViewById(R.id.tv_speaker);
DetailsOfLocation noLocationobj = mDetailsWithNoLocation.get(groupPosition);
lblListDesc.setText(noLocationobj.getDesc());
lblListRoom.setText("Room: "+noLocationobj.getLocation());
lblListAudience.setText("Audience: "+noLocationobj.getAudience());
lblListSpeaker.setText("Speaker/s: "+noLocationobj.getSpeaker());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
非常感谢您的帮助。
更新
我已经在我的 onGroupClick() 和 hashDataChild.get(
arrayList.get(groupPosition)) is null 为什么即使在检查 arrayList 时它有值也是这样?
我想我找到了使用这个的原因
if(!hashDataChild.containsKey(arrayList))
我的 hashDataChild 不包含密钥我不知道为什么以及如何解决它为什么会这样?即使在我调试时它确实显示了它的键和我的 arrayList 的值?
我已经更改了 containsKey 并按照建议使用了 String.valueOf(arrayList.get(groupPosition))
。但是我的 child 数据仍然没有显示,并且 onGroupClick()
中的 return false 它仍然关闭了我的应用程序。
好的,我试过你的代码,点击导致 NullPointerException,
您在每次调用 mDetailsWithNoLocation 时都错过了 String.valueOf(),
在您的适配器中更改这两个方法
@Override
public int getChildrenCount(int groupPosition) {
return this.mchildOfDetailsWithNoLocation.get(String.valueOf(
this.mDetailsWithNoLocation.get(groupPosition))).size();
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.mchildOfDetailsWithNoLocation.get(String.valueOf(
this.mDetailsWithNoLocation.get(groupPosition))).get(
childPosititon);
}
现在应该有工作了..
注意:您还应该 post 您 activity 在您的问题中编写代码,以使其他人更容易测试您的代码。我需要做一些猜测来测试这个..:)
我正在尝试创建一个 ExpandableListView,下面是我为可扩展列表创建数据的代码
DBHelper dbHelper;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
HashMap<String, List<String>> hashDataChild;
ArrayList<DetailsOfLocation> arrayList;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
expListView = (ExpandableListView) v.findViewById(R.id.lvExp); // get the listview
initializeDatabase();
prepareChildData();
listAdapter = new ExpandListAdapter(getActivity(), arrayList, hashDataChild);
expListView.setAdapter(listAdapter);
return v;
}
public void initializeDatabase()
{
dbHelper = new DBHelper(getActivity());
try {
dbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
private void prepareChildData(){
hashDataChild = new HashMap<String, List<String>>();
//arrayList has a multiple column in it where it will be displayed as the header. The data is from the database
Integer id = ((MyApplication) getActivity().getApplication()).getID();
arrayList = new ArrayList<DetailsOfLocation>();
arrayList = dbHelper.getList(id);
//child is the child's data
List<String> child = new ArrayList<String>();
child.add("Other Info");
child.add("Picture");
child.add("Contact");
//the header and child is being inserted
for(DetailsOfLocation list : arrayList){
hashDataChild.put(String.valueOf(list), child);
}
}
}
当我调试它时,hashDataChild 具有我期望的值,其中每个 header 有 3 个 child。但是在我的 phone 上尝试时它崩溃了并且在 logcat 中没有显示任何内容。我用于创建可扩展列表视图的参考是 [this]:http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
我在想可能是因为ExpandableListview OnGroupClickListener 没有工作?我该如何解决这个问题?它进入我的 onGroupClick 但不显示 child
的列表 // Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(
getActivity().getApplicationContext(),
arrayList.get(groupPosition)
+ " : "
+ hashDataChild.get(
arrayList.get(groupPosition)), Toast.LENGTH_SHORT)
.show();
return false;
}
});
下面是我的可扩展适配器代码
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<DetailsOfLocation> mDetailsWithNoLocation;
private HashMap<String, List<String>> mchildOfDetailsWithNoLocation;
public ExpandListAdapter(Context context, ArrayList<DetailsOfLocation> detailsWithNoLocation, HashMap<String, List<String>> HashChildData) {
this._context = context;
this.mDetailsWithNoLocation = detailsWithNoLocation; //header
this.mchildOfDetailsWithNoLocation = HashChildData; //hash data
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.mchildOfDetailsWithNoLocation.get(this.mDetailsWithNoLocation.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.expandlist_child_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.tvother_info);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.mchildOfDetailsWithNoLocation.get(this.mDetailsWithNoLocation.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.mDetailsWithNoLocation.get(groupPosition);
}
@Override
public int getGroupCount() {
return mDetailsWithNoLocation.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandlist_group_item, null);
}
TextView lblListDesc = (TextView) convertView.findViewById(R.id.tv_desc);
TextView lblListRoom = (TextView) convertView.findViewById(R.id.tv_location);
TextView lblListAudience = (TextView) convertView.findViewById(R.id.tv_audience);
TextView lblListSpeaker = (TextView) convertView.findViewById(R.id.tv_speaker);
DetailsOfLocation noLocationobj = mDetailsWithNoLocation.get(groupPosition);
lblListDesc.setText(noLocationobj.getDesc());
lblListRoom.setText("Room: "+noLocationobj.getLocation());
lblListAudience.setText("Audience: "+noLocationobj.getAudience());
lblListSpeaker.setText("Speaker/s: "+noLocationobj.getSpeaker());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
非常感谢您的帮助。
更新
我已经在我的 onGroupClick() 和 hashDataChild.get( arrayList.get(groupPosition)) is null 为什么即使在检查 arrayList 时它有值也是这样?
我想我找到了使用这个的原因
if(!hashDataChild.containsKey(arrayList))
我的 hashDataChild 不包含密钥我不知道为什么以及如何解决它为什么会这样?即使在我调试时它确实显示了它的键和我的 arrayList 的值?
我已经更改了 containsKey 并按照建议使用了 String.valueOf(arrayList.get(groupPosition))
。但是我的 child 数据仍然没有显示,并且 onGroupClick()
中的 return false 它仍然关闭了我的应用程序。
好的,我试过你的代码,点击导致 NullPointerException, 您在每次调用 mDetailsWithNoLocation 时都错过了 String.valueOf(), 在您的适配器中更改这两个方法
@Override
public int getChildrenCount(int groupPosition) {
return this.mchildOfDetailsWithNoLocation.get(String.valueOf(
this.mDetailsWithNoLocation.get(groupPosition))).size();
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.mchildOfDetailsWithNoLocation.get(String.valueOf(
this.mDetailsWithNoLocation.get(groupPosition))).get(
childPosititon);
}
现在应该有工作了.. 注意:您还应该 post 您 activity 在您的问题中编写代码,以使其他人更容易测试您的代码。我需要做一些猜测来测试这个..:)