Android layouts - programmatically setting value for a custom layout components

孤者浪人 提交于 2019-12-24 10:45:56

问题


I have defined a simple custom layout that includes a text view and an image view. in my main layout I want to use this layout several times and I want to add value to these text and image views in my code. (now manually, but later by taking data from database)

how can I access to these components in my code?

here is my main layout xml file

<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>

and the part in my adapter where I want to set data:

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:


You have to cast it to RelativeLayout. Go to comment_actions layout and check if the TextView has an ID set. If not, do so.

Then cast your likes, reply and stuff as RelativeLayout and find the TextView from there:

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


来源:https://stackoverflow.com/questions/17699196/android-layouts-programmatically-setting-value-for-a-custom-layout-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!