ExpandableListView Group 裡放了一個button,但是button監聽器如何擷取getGroupView裡的參數groupPosition呢?如果在button的監聽器裡直接使用groupPosition,會報錯,因為groupPosition不是final類型、、糾結了許久,在getGroupView裡把參數改成final就行啦、原來我還以為不能改Override的方法,不改不知道、、、
需求:因為group是一條評論、而且裡面還有個評論的button(二級評論),點擊按鈕後,把評論更新到該評論的child裡面作為二級評論
// 擷取一級列表View對象@Overridepublic View getGroupView(final int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {Log.v(TAG, "ExpandableList GroupView..");CommentLevel1 comment = mGroup.get(groupPosition);LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);RelativeLayout layout = (RelativeLayout) layoutInflater.inflate(R.layout.list_item_comment1, null);ImageView headPic;Button reply;TextView nickName, commentContent, postTime, commentNum, commentItem;headPic = (ImageView) layout.findViewById(R.id.list_item_talkCommentLevel1_iv_headPic);reply = (Button) layout.findViewById(R.id.list_item_talkCommentLevel1_btn_reply);nickName = (TextView) layout.findViewById(R.id.list_item_talkCommentLevel1_tv_nickName);commentContent = (TextView) layout.findViewById(R.id.list_item_talkCommentLevel1_tv_comment);postTime = (TextView) layout.findViewById(R.id.list_item_talkCommentLevel1_tv_postTime);commentNum = (TextView) layout.findViewById(R.id.list_item_talkCommentLevel1_tv_commentNum);commentItem = (TextView) layout.findViewById(R.id.comment);User user = comment.getUser();if (null != user) {headPic.setImageBitmap(user.getHeadPicture());nickName.setText(user.getNickName());}commentContent.setText(comment.getContent());postTime.setText(comment.getPostTime().toLocaleString());if (comment.getCommentNum() != 0) {commentNum.setText(String.valueOf(comment.getCommentNum()));} else {commentNum.setVisibility(View.INVISIBLE);commentItem.setVisibility(View.INVISIBLE);}// If the button get the focus, the list won't expand any more.reply.setFocusable(false);reply.setClickable(true);reply.setTag(groupPosition);reply.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {LayoutInflater factory = LayoutInflater.from(TalkListCommentsActivity.this);final View replyView = factory.inflate(R.layout.list_item_reply, null);new AlertDialog.Builder(TalkListCommentsActivity.this).setTitle(R.string.talk_postAComment).setView(replyView).setPositiveButton(R.string.positive,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int whichButton) {// User clicked OK so do some stuffCommentLevel2 level2;User user;Bitmap headPic;BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.myheadpic);headPic = bitmapDrawable.getBitmap();EditText commentText = (EditText) replyView.findViewById(R.id.talkList_et_reply);user = new User();level2 = new CommentLevel2();user.setNickName("使用者 x");user.setHeadPicture(headPic);level2.setUser(user);String strComment = commentText.getText().toString();if (null != strComment) {level2.setContent(strComment);}level2.setPostTime(new Date());mChild.get(groupPosition).add(level2);mAdapter.notifyDataSetChanged();}}).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int whichButton) {// User clicked cancel so do some// stuffdialog.dismiss();}}).create().show();}});return layout;}