ListView的使用

本文介绍了一个具体的ListView应用案例,包括实体类定义、布局文件设计及核心代码实现。通过创建新闻实体类News,设置主布局和选项布局文件,再配合自定义适配器NewsAdapter实现了动态填充ListView的数据。
摘要由CSDN通过智能技术生成

ListView的使用

1,实体类

package com.jackie.activity_05.Activity.entity;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by Administrator on 2016/11/2.
 */
public class News implements Serializable {

    private int id;
    private String image;
    private String title;
    private String content;
    private Date time;

    public News() {
    }

    public News(int id, String title, String content, Date time) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.time = time;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", image='" + image + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", time=" + time +
                '}';
    }
}

2,主布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />
</LinearLayout>

3,选项布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp">

    <ImageView
        android:id="@+id/news_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginRight="8dp"
        android:scaleType="centerInside"
        android:src="@drawable/a_8" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="2dp">

            <TextView
                android:id="@+id/news_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="新闻的标题"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/news_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120dp"
                android:text="9:30" />
        </LinearLayout>

        <TextView
            android:id="@+id/news_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp"
            android:ellipsize="end"
            android:maxLines="3"
            android:text="@string/newsContent" />
    </LinearLayout>
</LinearLayout>

4,核心代码

package com.jackie.activity_05.Activity.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jackie.activity_05.Activity.entity.News;
import com.jackie.activity_05.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2016/11/1.
 */
public class SecondActivity extends Activity {

    private ListView listView;
    private NewsAdapter newsAdapter;
    private List<News> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        listView = (ListView) findViewById(R.id.lv);

        datas = new ArrayList<>();
        datas.add(new News(0, "赵毅", "内容很长", new Date()));
        datas.add(new News(1, "赵毅2", "内容很长", new Date()));

        newsAdapter = new NewsAdapter(this, datas);
        listView.setAdapter(newsAdapter);
    }
}

class NewsAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<News> datas;

    public NewsAdapter(Context context, List<News> datas) {
        layoutInflater = LayoutInflater.from(context);
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return datas.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.activity_item_news, null);
            viewHolder = new ViewHolder();

            viewHolder.titel = (TextView) convertView.findViewById(R.id.news_title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.news_content);
            viewHolder.time = (TextView) convertView.findViewById(R.id.news_time);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.titel.setText(datas.get(position).getTitle());
        viewHolder.content.setText(datas.get(position).getContent());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日 HH:mm", Locale.CHINA);
        viewHolder.time.setText(simpleDateFormat.format(datas.get(position).getTime()));
        return convertView;
    }
}

class ViewHolder {
    TextView titel;
    TextView content;
    TextView time;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值