2012-04-28 50 views
0

我在非活動類中構建動態視圖,該活動類由我的活動類調用,以填充我的主視圖的一部分。我的問題發生在這個「子視圖」內android:如何在動態視圖中從onclicklistener更改

我有一個onclicklistener,它將項目添加到列表視圖,但我不能從onclicklistener調用適配器上的notifydatasetchanged,因爲適配器對象需要聲明爲final,而不會工作。

我在做什麼錯?

這裏是我的代碼:(我想notifydatasetchanged之後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; 
} 

回答

1

讓你的非活性類的構造函數接收適配器參考,並保存在實例變量在您的非活動類。

然後,它隨處類中使用未聲明的最後

0

試試下面的代碼:

myListView.invalidateViews(); 
相關問題