单击ExpandableListview Android

时间:2015-07-20 04:10:45

标签: android hashmap expandablelistview keyset

我试图创建一个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中显示任何内容。我用于创建可展开列表视图的参考是[this]: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中返回true(                                     arrayList.get(groupPosition))为null为什么它是这样的,即使在检查arrayList时它有值?

我想我找到了使用此

的原因
if(!hashDataChild.containsKey(arrayList))

我的hashDataChild不包含密钥我不知道为什么以及如何解决它为什么会这样?即使我在调试时它确实显示了我的arrayList的键和值吗?

我已更改containsKey并按建议使用String.valueOf(arrayList.get(groupPosition))。但是我的孩子数据仍然没有出现并在onGroupClick()中返回false,它仍然会关闭我的应用程序。

1 个答案:

答案 0 :(得分:1)

好的,我已经尝试过你的代码,点击导致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);
}

现在应该有工作.. 注意:您还应该在您的问题中发布活动代码,以便让其他人更轻松地测试您的代码。我需要做一些猜测来测试这个.. :))