场景
Android中使用Adapter(适配器)给RecycleView设置数据源:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/110926089
之前讲过使用适配器给RecycleView进行设置数据源,可能对于新手不是很友好。
如果只是想简单的在页面中显示一个对象list的某属性值怎样使用适配器给listView设置数据源。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先在布局文件中添加一个ListView组件
<ListView android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="match_parent" />
然后再新建一个Bean用来存储数据对象。
package com.badao.badaoimclient.bean; import android.support.annotation.NonNull; import java.util.List; public class ChatBean { private String msg; private int code; @NonNull private String id = ""; private List<ChatItem> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public List<ChatItem> getData() { return data; } public void setData(List<ChatItem> data) { this.data = data; } @NonNull public String getId() { return id; } public void setId(@NonNull String id) { this.id = id; } public static class ChatItem { private int id; private String msgNum; private String content; //语音消息服务器地址 private String remoteContent; private String sender; private String receiver; private String type; private boolean canReceived; private String sendTime; private String receivedTime; //语音时长 private int voiceDuration; private boolean isRead; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMsgNum() { return msgNum; } public void setMsgNum(String msgNum) { this.msgNum = msgNum; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isCanReceived() { return canReceived; } public void setCanReceived(boolean canReceived) { this.canReceived = canReceived; } public String getSendTime() { return sendTime; } public void setSendTime(String sendTime) { this.sendTime = sendTime; } public String getReceivedTime() { return receivedTime; } public void setReceivedTime(String receivedTime) { this.receivedTime = receivedTime; } public int getVoiceDuration() { return voiceDuration; } public void setVoiceDuration(int voiceDuration) { this.voiceDuration = voiceDuration; } public String getRemoteContent() { return remoteContent; } public void setRemoteContent(String remoteContent) { this.remoteContent = remoteContent; } public boolean isRead() { return isRead; } public void setRead(boolean read) { isRead = read; } } }
具体要使用的字段根据自己的需要。
然后在listview所在布局文件对应的activity中
首先先定义一个内部类的适配器
privateMyAdapter myAdapter;
适配器的声明在activity的内部类中声明
/** * 定义一个适配器 */ private class MyAdapter extends BaseAdapter { private List<ImUserBean.RowsBean> rowsBeanList ; public MyAdapter(List<ImUserBean.RowsBean> rowsBeanList){ this.rowsBeanList = rowsBeanList; } // 返回需要显示的条目数 @Override public int getCount() { return rowsBeanList==null?0:rowsBeanList.size(); } @Override public Object getItem(int position) { return rowsBeanList.get(position); } @Override public long getItemId(int position) { return 0; } /** * 获取一个View用来显示ListView的数据,会作为ListView的一个条目显示 * @param position 当前需要显示的View的索引 * @param convertView 或存数据的对象 * @param parent * @return 返回需要显示的View */ @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = new TextView(AddressBookActivity.this); //设置每一项显示的内容 textView.setText(rowsBeanList.get(position).getImNumX()+"-----"+((int)rowsBeanList.get(position).getCanOnlineX()==1?"在线":"不在线")); //设置每一项的点击事件 textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(AddressBookActivity.this,SendMessageActivity.class); intent.putExtra("imnum",rowsBeanList.get(position).getImNumX()); startActivity(intent); } }); return textView; } }
使适配器扩展自BaseAdapter并重写其方法,在内部类中
声明数据源的实体bean的list,然后在内部类适配器的构造方法中对其进行赋值。
在getCount方法中设置显示的条目数为数据源的长度。
在getItem中设置每一项为数据源对应下标的每一项。
在getView中设置一个视图textView来显示listView的数据,它将作为ListView的一个条目进行显示。
并且还可以设置每一项的显示的文本内容和点击事件。
然后在onCreate方法中异步调用接口获取数据源,并通过适配器的构造方法给
适配器中的数据源赋值。
然后给通过id获取到的listView设置适配器。
activity完整代码:
package com.badao.badaoimclient.view; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.badao.badaoimclient.R; import com.badao.badaoimclient.bean.ImUserBean; import com.badao.badaoimclient.web.WebClient; import java.io.IOException; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class AddressBookActivity extends AppCompatActivity { private ListView listView; private List<ImUserBean.RowsBean> rowsBeanList; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_address_book); listView = findViewById(R.id.lv_list); //异步请求通讯录 WebClient.getInstance().getImUserList().enqueue(new Callback<ImUserBean>() { @Override public void onResponse(Call<ImUserBean> call, Response<ImUserBean> response) { Log.i("response",response.toString()); if(response.code()==200) { ImUserBean userBean = response.body(); rowsBeanList = userBean.getRows(); myAdapter = new MyAdapter(rowsBeanList); listView.setAdapter(myAdapter); } } @Override public void onFailure(Call<ImUserBean> call, Throwable t) { Log.i("onFailure",t.toString()); } }); } /** * 定义一个适配器 */ private class MyAdapter extends BaseAdapter { private List<ImUserBean.RowsBean> rowsBeanList ; public MyAdapter(List<ImUserBean.RowsBean> rowsBeanList){ this.rowsBeanList = rowsBeanList; } // 返回需要显示的条目数 @Override public int getCount() { return rowsBeanList==null?0:rowsBeanList.size(); } @Override public Object getItem(int position) { return rowsBeanList.get(position); } @Override public long getItemId(int position) { return 0; } /** * 获取一个View用来显示ListView的数据,会作为ListView的一个条目显示 * @param position 当前需要显示的View的索引 * @param convertView 或存数据的对象 * @param parent * @return 返回需要显示的View */ @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = new TextView(AddressBookActivity.this); //设置每一项显示的内容 textView.setText(rowsBeanList.get(position).getImNumX()+"-----"+((int)rowsBeanList.get(position).getCanOnlineX()==1?"在线":"不在线")); //设置每一项的点击事件 textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(AddressBookActivity.this,SendMessageActivity.class); intent.putExtra("imnum",rowsBeanList.get(position).getImNumX()); startActivity(intent); } }); return textView; } } }
博客园:
https://www.cnblogs.com/badaoliumangqizhi/
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。