简体   繁体   中英

ExpandableListView Change layout of first child

I'm trying to load two different layouts into my expandable list child . I keep getting a null pointer, which to me sounds like my !=0 is not actually working the way I intended. What am I doing wrong?

   public View getChildView(final int groupPosition, final int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    Log.e("COMMENTS", "Ok so the POSITION: " + childPosition);
    if (convertView == null) {
        if(childPosition==0){
            //first view
            convertView = inflater.inflate(R.layout.comments_create_comment, null);
        }else {
            //second view
            convertView = inflater.inflate(R.layout.comments_expandable_list_child, null);
        }
    }
    if(childPosition!=0){
        TextView item = (TextView) convertView.findViewById(R.id.comments_expandable_list_child_text_view);
        ImageView delete = (ImageView) convertView.findViewById(R.id.comments_expandable_list_child_delete);
        //throwing a null pointer exception at the delete.setonclick 
        //I'm guessing it's still using the first view here somehow?
        delete.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                List<String> child = comments_feed_collection.get(group_list.get(groupPosition));
                child.remove(childPosition);
                notifyDataSetChanged();
            }
        });
        item.setText(laptop);
    }
    return convertView;
}

Exact print out:

04-22 10:27:58.762  21013-21013/com.myapp E/COMMENTS﹕ Ok so the POSITION: 0
04-22 10:27:58.772  21013-21013/com.myapp I/Editor﹕ setup window support handles
04-22 10:27:58.772  21013-21013/com.myapp E/COMMENTS﹕ Ok so the POSITION: 1
04-22 10:27:58.772  21013-21013/com.myapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4183fda0)
04-22 10:27:58.792  21013-21013/com.myapp E/oh nooo﹕ java.lang.NullPointerException
            at comments.CommentsExpandableListAdapter.getChildView(CommentsExpandableListAdapter.java:58)

your problem lies here:

 if (convertView == null) {
        if(childPosition==0){
            //first view
            convertView = inflater.inflate(R.layout.comments_create_comment, null);
        }else {
            //second view
            convertView = inflater.inflate(R.layout.comments_expandable_list_child, null);
        }
 }

convertView is null getChildTypeCount times. If you didn't override this method, convertView will be inflated just once. Most probably for your childPosition==0 case. You can find here the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM