import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class ChatAdapter extends BaseAdapter { private List<ChatBean> chatBeanList; private LayoutInflater layoutInflater; public ChatAdapter(List<ChatBean> chatBeanList, Context context){ this.chatBeanList=chatBeanList; layoutInflater=LayoutInflater.from(context); } @Override public int getCount() { return chatBeanList.size(); } @Override public Object getItem(int position) { return chatBeanList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View conventView, ViewGroup parent) { Holder holder=new Holder(); if (chatBeanList.get(position).getState()==ChatBean.RECEIVE){ conventView=layoutInflater.inflate(R.layout.chatting_left_item,null); }else { conventView=layoutInflater.inflate(R.layout.chatting_right_item,null); } holder.tv_chat_content=(TextView) conventView.findViewById(R.id.tv_chat_content); holder.tv_chat_content.setText(chatBeanList.get(position).getMessage()); return conventView; } class Holder{ public TextView tv_chat_content; } }有什么作用
时间: 2024-03-30 07:37:52 浏览: 69
Kotlin案例分析.docx
这是一个适配器(Adapter)类,用于将数据源(chatBeanList)中的数据适配到ListView或GridView等控件上显示。具体来说,它会根据数据源中每个ChatBean对象的状态(是接收还是发送)来决定使用哪个布局(chatting_left_item或chatting_right_item),然后将对应的聊天内容显示在布局中的TextView上。通过getView()方法返回的View对象,最终会被显示在ListView或GridView的每一行上。
阅读全文