public class MainActivity extends AppCompatActivity {
// private TextView text1;
private NewsAdapter adapter;
private ListView list_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list_view.setAdapter(adapter);
}
private void initView() {
list_view = (ListView) findViewById(R.id.listView);
adapter=new NewsAdapter(this);
OkhttpUtil.getRequest(OkhttpUtil.URL, new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d("Failure msg", "onFailure:" + e.getMessage());
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
final String res = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
// text1.setText(Html.fromHtml(res));
NewsBean newsBeanJson = JsonParse.getInstance().getNewsList(res);
List rowsList = newsBeanJson.getRows();
if (rowsList != null) {
if (rowsList.size() > 0) {
adapter.setData(rowsList);
}
}
}
});
}
});
}
}
public class NewsAdapter extends BaseAdapter {
private List rowsList = new ArrayList();
private Context context;
private LayoutInflater layoutInflater;
public void setData(List rowsList) {
for (RowsBean rowsBean : rowsList) {
Log.i("rowsList",rowsBean.toString());
}
this.rowsList = rowsList;
notifyDataSetChanged();
}
public NewsAdapter(Context context) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return rowsList.size();
}
@Override
public RowsBean getItem(int position) {
return rowsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.item, null);
convertView.setTag(new ViewHolder(convertView));
}
initializeViews((RowsBean) getItem(position), (ViewHolder) convertView.getTag());
return convertView;
}
private void initializeViews(RowsBean rowsBean, ViewHolder holder) {
Glide.with(context).load("http://124.93.196.45:10001/"+rowsBean.getCover()).error(R.mipmap.ic_launcher).into(((ViewHolder) holder).img);
holder.tvTitle.setText(rowsBean.getTitle());
holder.tvCreateTime.setText(rowsBean.getCreateTime());
holder.tvReadNum.setText(rowsBean.getReadNum()+"");
}
protected class ViewHolder {
private ImageView img;
private TextView tvTitle;
private TextView tvCreateTime;
private TextView tvReadNum;
public ViewHolder(View view) {
img = (ImageView) view.findViewById(R.id.img);
tvTitle = (TextView) view.findViewById(R.id.tv_title);
tvCreateTime = (TextView) view.findViewById(R.id.tv_createTime);
tvReadNum = (TextView) view.findViewById(R.id.tv_readNum);
}
}
}
public class NewsBean implements Serializable {
private static final long serialVersionUID = 1L;
private int total;
private int code;
private String msg;
private List rows;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
@Override
public String toString() {
return "NewsBean{" +
"total=" + total +
", code=" + code +
", msg='" + msg + ''' +
", rows=" + rows +
'}';
}
}
public class RowsBean implements Serializable {
private static final long serialVersionUID = 8656535106339848159L;
private Object searchValue;
private String createBy;
private String createTime;
private String updateBy;
private String updateTime;
private Object remark;
private ParamsBean params;
private int id;
private String appType;
private String cover;
private String title;
private Object subTitle;
private String content;
private String status;
private String publishDate;
private Object tags;
private int commentNum;
private int likeNum;
private int readNum;
private String type;
private String top;
private String hot;
public Object getSearchValue() {
return searchValue;
}
public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public Object getRemark() {
return remark;
}
public void setRemark(Object remark) {
this.remark = remark;
}
public ParamsBean getParams() {
return params;
}
public void setParams(ParamsBean params) {
this.params = params;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAppType() {
return appType;
}
public void setAppType(String appType) {
this.appType = appType;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Object getSubTitle() {
return subTitle;
}
public void setSubTitle(Object subTitle) {
this.subTitle = subTitle;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public Object getTags() {
return tags;
}
public void setTags(Object tags) {
this.tags = tags;
}
public int getCommentNum() {
return commentNum;
}
public void setCommentNum(int commentNum) {
this.commentNum = commentNum;
}
public int getLikeNum() {
return likeNum;
}
public void setLikeNum(int likeNum) {
this.likeNum = likeNum;
}
public int getReadNum() {
return readNum;
}
public void setReadNum(int readNum) {
this.readNum = readNum;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTop() {
return top;
}
public void setTop(String top) {
this.top = top;
}
public String getHot() {
return hot;
}
public void setHot(String hot) {
this.hot = hot;
}
public static class ParamsBean {
}
@Override
public String toString() {
return "RowsBean{" +
"searchValue=" + searchValue +
", createBy='" + createBy + ''' +
", createTime='" + createTime + ''' +
", updateBy='" + updateBy + ''' +
", updateTime='" + updateTime + ''' +
", remark=" + remark +
", params=" + params +
", id=" + id +
", appType='" + appType + ''' +
", cover='" + cover + ''' +
", title='" + title + ''' +
", subTitle=" + subTitle +
", content='" + content + ''' +
", status='" + status + ''' +
", publishDate='" + publishDate + ''' +
", tags=" + tags +
", commentNum=" + commentNum +
", likeNum=" + likeNum +
", readNum=" + readNum +
", type='" + type + ''' +
", top='" + top + ''' +
", hot='" + hot + ''' +
'}';
}
}
public class JsonParse {
public static JsonParse instance;
public JsonParse() {
}
public static JsonParse getInstance() {
if (instance == null) {
instance = new JsonParse();
}
return instance;
}
public NewsBean getNewsList(String json) {
Gson gson = new Gson();
Type listType = new TypeToken() {
}.getType();
NewsBean newsBeanJson = gson.fromJson(json, listType);
return newsBeanJson;
}
}