我正在一个非活动类中构建一个动态视图,我的活动类调用它来填充我的主视图的一部分。我的问题发生在这个“子视图”中
我有一个 onclicklistener 可以将项目添加到列表视图,但是我不能从 onclicklistener 调用适配器上的 notifydatasetchanged,因为需要将适配器对象声明为 final,这将不起作用。
我究竟做错了什么?
这是我的代码:(我想在newComment(o)之后立即通知数据集更改)
public RelativeLayout getCommentsView(final Object o) {
RelativeLayout view = new RelativeLayout(mContext);
ImageView background = new ImageView(mContext);
background.setImageResource(R.drawable.background);
background.setScaleType(ScaleType.FIT_START);
LinearLayout comments = new LinearLayout(mContext);
comments.setOrientation(LinearLayout.VERTICAL);
comments.setPadding(8,20,8,0);
LinearLayout titleBar = new LinearLayout(mContext);
ImageView addButton = new ImageView(mContext);
addButton.setImageResource(R.drawable.ic_menu_add);
titleBar.setClickable(true);
titleBar.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View view) {
newComment(o);
}
});
TextView title = new TextView(mContext);
title.setText(mContext.getString(R.string.comments));
title.setPadding(10,5,0,0);
title.setTextSize(18);
title.setTextColor(Color.WHITE);
if (mContext instanceof LocationActivity) {
title.setTextColor(Color.parseColor("#33b5e5"));
} else if (mContext instanceof TrailActivity) {
title.setTextColor(Color.parseColor("#AA66CC"));
}
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
titleBar.addView(title,layout);
layout = new LinearLayout.LayoutParams(48,48);
titleBar.addView(addButton,layout);
TextView divider = new TextView(mContext);
divider.setBackgroundResource(R.drawable.divider);
ListView commentsList = new ListView(mContext);
commentsList.setBackgroundColor(Color.TRANSPARENT);
commentsList.setCacheColorHint(Color.TRANSPARENT);
commentsList.addHeaderView(titleBar);
commentsList.addFooterView(divider);
CommentsListAdapter commentsListAdapter = new CommentsListAdapter(mContext,R.layout.comments_list_item,getCommentsFor(o));
commentsList.setAdapter(commentsListAdapter);
comments.addView(commentsList);
view.addView(background);
view.addView(comments);
return view;
}