2013-07-17 123 views
0

我已經定義了一個包含文本視圖和圖像視圖的簡單自定義佈局。在我的主佈局中,我想多次使用此佈局,並且希望在我的代碼中爲這些文本和圖像視圖添加值。 (現在手動,但後來通過從數據庫中獲取數據)Android佈局 - 以編程方式設置自定義佈局組件的值

如何在我的代碼中訪問這些組件?

這是我的主要佈局的xml文件

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/comment_content" 
    android:orientation="horizontal" > 

    <!-- these four components have the same structure but different styles and behaviors. --> 
    <include 
     android:id="@+id/like" 
     style="@style/LikeStyle" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     layout="@layout/comment_actions" /> 

    <include 
     android:id="@+id/dislike" 
     style="@style/DislikeStyle" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     layout="@layout/comment_actions" /> 

    <include 
     android:id="@+id/reply" 
     style="@style/ReplyStyle" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     layout="@layout/comment_actions" /> 

    <include 
     android:id="@+id/share" 
     style="@style/ShareStyle" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     layout="@layout/comment_actions" /> 
</LinearLayout> 

,並在我的適配器的一部分,我想設置數據:

public View getView(int position, View view, ViewGroup parent) { 
    ViewHolder holder = null; 
    if (view == null) { 
     LayoutInflater infalInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = infalInflater.inflate(R.layout.comment_item, null); 
     holder = new ViewHolder(); 
     holder.userName = (TextView) view.findViewById(R.id.user_name); 
     holder.userImage = (ImageView) view.findViewById(R.id.user_image); 
     holder.commentContent = (TextView) view.findViewById(R.id.comment_content); 
     holder.commentTitle = (TextView) view.findViewById(R.id.comment_title); 

     // these tree below layouts are custom. now when running my app i recieve a class cast exception 
     // because r.id.like and other twos are relative layout. 
     holder.likes = (TextView) view.findViewById(R.id.like); 
     holder.dislikes = (TextView) view.findViewById(R.id.dislike); 
     holder.replys = (TextView) view.findViewById(R.id.reply); 
     view.setTag(holder); 
    } 
    else { 
     holder = (ViewHolder) view.getTag(); 
    } 
    holder.userName.setText(((Comment) listData.get(position)).getUsername()); 
    holder.commentContent.setText(((Comment)listData.get(position)).getContent()); 
    holder.commentTitle.setText(((Comment)listData.get(position)).getTitle()); 

    // the part where I want to set data for these custom layouts. 
    holder.likes.setText(((Comment) listData.get(position)).getLikes()); 
    holder.dislikes.setText(((Comment) listData.get(position)).getDislikes()); 
    holder.replys.setText(((Comment) listData.get(position)).getReplys()); 
    return view; 
} 
+1

更好的方法是編寫自定義視圖類,有自己的佈局和屬性。 –

+0

謝謝。我會嘗試。 –

回答

0

你必須將它轉換爲RelativeLayout。請轉至comment_actions佈局,並檢查TextView是否設置了ID。如果沒有,那就這樣做。

然後從那裏投你所好,回覆和東西爲RelativeLayout,找到TextView

RelativeLayout rlLikes = (RelativeLayout) view.findViewById(R.id.like); 
holder.likes = (TextView) rlLikes.findViewById(R.id.idOfTextViewInsideComment_actions); 
+0

是的。 TextView和ImageView都有ID。非常感謝你幫助我。 –

+0

這工作,現在我有正確的值設置。但是,當我跑我看到一個數字代碼(喜歡數),而不是4個數字和4幅4圖像(似乎他們都在彼此的頂部) 你有什麼想法。那是爲什麼? –

+0

不,不是真的。我想這與'comment_actions'裏面的佈局有關。 –