申请停止
我试图创建一个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有我期待的每个头里有3个孩子的值。但在我的手机上试用时,它会崩溃,并且不会在logcat中显示任何内容。我使用的是用于创建可扩展列表视图的引用[这]:http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
我在想,这可能是因为ExpandableListview OnGroupClickListener不工作?我该如何解决这个问题?它去我onGroupClick内,但没有显示孩子的名单
// 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))为空,为什么会这样,即使检查的ArrayList时,它具有值?
我想我找到了原因,采用这种
if(!hashDataChild.containsKey(arrayList))
我hashDataChild不包含关键我不知道为什么,以及如何解决它的任何想法,为什么是这样呢?即使当我调试它显示它的键和我的arrayList的值?
我已经改变的containsKey和使用String.valueOf(arrayList.get(groupPosition))
的建议。但是我的孩子数据仍然不显示,并在onGroupClick()
中返回false,它仍然关闭我的应用程序。
好吧,我已经试过你的代码和点击导致NullPointerException异常, 你错过了将String.valueOf()在你的mDetailsWithNoLocation的每一个电话, 在适配器更改这两个方法
@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);
}
现在应该有工作.. 笔记:你也应该在你的问题中发布你的活动代码,以使其他人更容易测试你的代码。我需要做一些猜测,以测试这.. :)
非常感谢您的支持!它真的帮助我很多:)我会把我的活动代码。我在我的一个标签中使用它,这就是为什么我只发布我的片段。最后一个问题如何控制每个孩子被点击时? –
不客气,你的意思是ExpandableListView.setOnChildClickListener()? – fchristysen
是的..我很困惑如何使用它,因为我试着把它敬酒,然后当我点击标题/父母它会触发代码,但我期待它只会触发当我点击子项目。 –
的机会是'NPE'。请在'dbHelper.getList(id);'后检查'arrayList'的值。它可能返回'null'。 –
什么是NPE? @ViswanathL –
我使用吐司检查了我的ArrayList和它的数据在它@ViswanathL –