如何使用适配器的方法voidbind()在cardview中显示多个Textcard

时间:2018-10-09 15:22:38

标签: java android android-recyclerview adapter android-adapter

这是我的代码:

public class MessageListAdapter extends RecyclerView.Adapter {
    public static String TAG = MessageListAdapter.class.getName();

    private static final int VIEW_TYPE_MESSAGE_SENT = 1;
    private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
     private static final int VIEW_TYPE_ACTIONCARD = 3;
    private static final int VIEW_TYPE_TEXTCARD = 5;
    private static final int VIEW_TYPE_IMAGECARD = 4;
    private Context mContext;
    private List<Message> mMessageList;
    private ChipClick mChipClick;



    public MessageListAdapter(Context context, List<Message> messageList, List<TextCard> developersLists, ChipClick chipClick) {
        //public MessageListAdapter(Context context, List<Message> messageList, ChipClick chipClick) {
        mContext = context;
        mMessageList = messageList;
        dev  = developersLists;
        mChipClick = chipClick;
        developersLists = new ArrayList<>();
    }

    @Override
    public int getItemCount() {
        return mMessageList.size();
    }

    // Determines the appropriate ViewType according to the sender of the message.
    @Override
    public int getItemViewType(int position) {
        Message message = (Message) mMessageList.get(position);
        if (message.isSenderBot()) {
            // If the current user is the sender of the message
            return VIEW_TYPE_MESSAGE_SENT;
        }
        else if (message.getImageCard() != null){
            return  VIEW_TYPE_IMAGECARD;
        }
        else if ( message.getTextCard() != null){  //message.getTextCard()
            return  VIEW_TYPE_TEXTCARD;
        }
        else {
            // If some other user sent the message
            return VIEW_TYPE_MESSAGE_RECEIVED;
        }

    }

    // Inflates the appropriate layout according to the ViewType.
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        if (viewType == VIEW_TYPE_MESSAGE_SENT) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_sent, parent, false);
            return new SentMessageHolder(view);
        } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_redeived, parent, false);
            return new ReceivedMessageHolder(view);
        }
        else if (viewType == VIEW_TYPE_IMAGECARD) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.imagecard, parent, false);
            return new ImageCardHolder(view);
        }
        else if (viewType == VIEW_TYPE_TEXTCARD) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.textcard, parent, false);
            return new TextCardHolder(view);
        }
        return null;
    }

    // Passes the message object to a ViewHolder so that the contents can be bound to UI.
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Message message = (Message) mMessageList.get(position);

        switch (holder.getItemViewType()) {
            case VIEW_TYPE_MESSAGE_SENT:
                ((SentMessageHolder) holder).bind(message);
                break;
            case VIEW_TYPE_MESSAGE_RECEIVED:
                ((ReceivedMessageHolder) holder).bind(message);
                break;
            case VIEW_TYPE_IMAGECARD:
                ((ImageCardHolder) holder).bind(message);
                break;
            case VIEW_TYPE_TEXTCARD:
                ((TextCardHolder)holder).bind(message,position);


        }
    }

    private class SentMessageHolder extends RecyclerView.ViewHolder {
        TextView messageText, timeText;

        SentMessageHolder(View itemView) {
            super(itemView);
            messageText = (TextView) itemView.findViewById(R.id.text_message_body);
            timeText = (TextView) itemView.findViewById(R.id.text_message_time);
        }

        void bind(Message message) {
            messageText.setText(message.getMessage());
            timeText.setText(Util.formatDateTime(message.getCreatedAt()));
        }
    }

    private class ReceivedMessageHolder extends RecyclerView.ViewHolder {

        ConstraintLayout constraintLayout;

        ImageView profileImage;
        TextView textMessage;
        TextView timeText;

        ChipGroup chipGroup;
        Chip chip;

        ReceivedMessageHolder(View itemView) {
            super(itemView);
            constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.parentCard);

            profileImage = (ImageView) itemView.findViewById(R.id.image_message_profile);
            textMessage = (TextView) itemView.findViewById(R.id.text_message);
            timeText = (TextView) itemView.findViewById(R.id.text_message_time);

            chipGroup = (ChipGroup) itemView.findViewById(R.id.chipGroup);
        }

        void bind(final Message message) {         

                textMessage.setText(message.getMessage());
            timeText.setText(Util.formatDateTime(message.getCreatedAt()));
        }

    }   

    private class TextCardHolder extends RecyclerView.ViewHolder {

        TextView Textcardtitle;
        TextView Textcardcontent;

        public TextCardHolder(View view) {
            super(view);
            Textcardtitle = (TextView) view.findViewById(R.id.textcardTitle);
            Textcardcontent = (TextView)view.findViewById(R.id.textcardContent);
        }

        public void bind(Message message, int position) {

            Textcardtitle.setText(message.getTextCard().iterator().next().getTitle());
            Textcardcontent.setText(message.getTextCard()
                    .iterator().next().getDescriptions()
                    .iterator().next().getDescription());

           for (int i = 0; i < message.getTextCard().size(); i++) {
                Log.d(TAG,"Descriptions1 "+message.getTextCard().listIterator(i).next().
                        getDescriptions().iterator().next().getDescription() +
                        " TITLE   " +message.getTextCard().listIterator(i).next().getTitle());
            }
        }


    }

}

这是我的完整适配器代码。 消息对象具有文本卡列表,该列表具有多个值。但是void bind()仅显示一个文本卡,而不显示多个。

如何显示所有具有值,标题和说明的多张卡。我尝试在loop中,它在logcat中循环显示所有值,但仅显示最后一个值。我尝试在循环内设置文本,但仍然无法显示。

我的消息模型类具有:  List<TextCard> textCard;

TextCard Model类具有:

@SerializedName("Title") @Expose private String Title @SerializedName("content") @Expose private String content; @SerializedName("Descriptions") @Expose private List<TextCardDescription> descriptions = null;

TextCard描述具有: @SerializedName("Description") @Expose private String Description;

我正在使用Parcalable解析api服务中的所有数据。

请帮助我显示ArrayList中的多个文本卡, 因为我是Android新手

日志:

  

Descriptions1佩什瓦人住的地方将保留标题“ Shaniwar Wada”

     

Shivaji Maharaj TITLE Sinhagad所描述的1辆福特精装车

0 个答案:

没有答案