Android ListView与适配器

简介: 练习:实现如下图新闻列表, 并且添加点赞,且点击点赞、点攒数增加, 点击图片显示已点击了图片。 布局: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:o

练习:实现如下图新闻列表,

并且添加点赞,且点击点赞、点攒数增加,

点击图片显示已点击了图片。


布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:gravity="center"
    android:text="标题"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/from"
    android:layout_marginStart="96dp"
    android:layout_marginTop="19dp" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/from"
    android:text="未来网"
    android:layout_alignBaseline="@+id/pubData"
    android:layout_alignBottom="@+id/pubData"
    android:layout_alignParentStart="true" />
    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/iv"
        android:scaleType="centerCrop"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pubData"
        android:text="今天"
        android:layout_below="@+id/iv"
        android:layout_alignStart="@+id/iv"
        android:layout_marginStart="14dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点赞"
        android:id="@+id/good"
        android:layout_alignTop="@+id/goodcount"
        android:layout_alignStart="@+id/title" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1000"
        android:id="@+id/goodcount"
        android:layout_below="@+id/iv"
        android:layout_toEndOf="@+id/title" />
</RelativeLayout>

class文件:

package com.example.administrator.jreduch05.entity;

/**
 * Created by Administrator on 2016/8/3.
 */
public class News {

    private String title;
    private String pubData;
    private String newsId;
    private int img;
    private String from;
    private  String good;
    private int goodcount;
    public News(){}

    public News(String title, String pubData, String newsId, int img, String from, String good, int goodcount) {
        this.title = title;
        this.pubData = pubData;
        this.newsId = newsId;
        this.img = img;
        this.from = from;
        this.good = good;
        this.goodcount = goodcount;
    }

    public String getTitle() {
        return title;
    }

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

    public String getPubData() {
        return pubData;
    }

    public void setPubData(String pubData) {
        this.pubData = pubData;
    }

    public String getNewsId() {
        return newsId;
    }

    public void setNewsId(String newsId) {
        this.newsId = newsId;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getGood() {
        return good;
    }

    public void setGood(String good) {
        this.good = good;
    }

    public int getGoodcount() {
        return goodcount;
    }

    public void setGoodcount(int goodcount) {
        this.goodcount = goodcount;
    }
}

适配器:

package com.example.administrator.jreduch05.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.jreduch05.R;
import com.example.administrator.jreduch05.entity.News;

import java.util.List;

/**
 * Created by Administrator on 2016/8/3.
 */
public class MyListAdapter extends BaseAdapter{

    private List<News> myData;
    private Context context;

    public MyListAdapter(Context context,List<News>myData){
        this.myData=myData;
        this.context=context;
    }
    @Override
    public int getCount() {
        return myData.size();
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if(convertView==null){
            vh=new ViewHolder();
            convertView=LayoutInflater.from(context).inflate(R.layout.list_layout_base,null);
            vh.title=  (TextView) convertView.findViewById(R.id.title);
            vh.pubData=  (TextView) convertView.findViewById(R.id.pubData);
            vh.from=  (TextView) convertView.findViewById(R.id.from);
            vh.img  =(ImageView) convertView.findViewById(R.id.iv);
            vh.good  =(TextView) convertView.findViewById(R.id.good);

            convertView.setTag(vh);
        }else{
            vh=(ViewHolder)convertView.getTag();
        }
        final News ns=myData.get(position);
        vh.good.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count=Integer.parseInt(vh.goodCount.getText().toString());
                vh.goodCount.setText(count+1+"");
                ns.setGoodcount(count);
                //vh.goodCount.setTextColor();
            }
        });
        vh.goodCount  =(TextView) convertView.findViewById(R.id.goodcount);
        vh.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"点击了图片"+position,
                        Toast.LENGTH_SHORT).show();
            }
        });
        vh.title.setText(ns.getTitle());
        vh.pubData.setText(ns.getPubData());
        vh.from.setText(ns.getFrom());
        vh.img.setImageResource(ns.getImg());
        vh.good.setText(ns.getGood());
        vh.goodCount.setText(ns.getGoodcount()+"");

      return convertView;
    }

    public class ViewHolder{
        TextView title;
        TextView pubData;
        TextView from;
        ImageView img;
        TextView good;
        TextView goodCount;
    }
}

Activity主文件:

package com.example.administrator.jreduch05;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.example.administrator.jreduch05.adapter.MyListAdapter;
import com.example.administrator.jreduch05.entity.News;

import java.util.ArrayList;
import java.util.List;

public class List3Activity extends AppCompatActivity {
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list3);
        lv=(ListView)findViewById(R.id.lv);

        List <News>list=new ArrayList();
        News ns=new News("新闻1","刚刚","001",R.mipmap.dianzan,"新浪网","点赞",10);
        list.add(ns);

        ns=new News("新闻2","今天","002",R.mipmap.dianzan,"未来网","点赞",11);
        list.add(ns);

        ns=new News("新闻2","今天","003",R.mipmap.dianzan,"搜狐网","点赞",12);
        list.add(ns);

        ns=new News("新闻3","今天","004",R.mipmap.dianzan,"网易网","点赞",13);
        list.add(ns);

        ns=new News("新闻4","昨天","005",R.mipmap.dianzan,"天涯网","点赞",14);
        list.add(ns);

        lv.setAdapter(new MyListAdapter(this,list));
    }
}

目录
相关文章
|
6月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
81 2
|
5月前
|
API Android开发 开发者
`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView
【6月更文挑战第26天】`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView。它提供高效的数据视图复用,优化的布局管理,支持多种布局(如线性、网格),并解耦数据、适配器和视图。RecyclerView的灵活性、性能(如局部刷新和动画支持)和扩展性使其成为现代Android开发的首选,特别是在处理大规模数据集时。
62 2
|
6月前
|
XML Java Android开发
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
661 1
|
5月前
|
XML 前端开发 Java
24. 【Android教程】适配器 Adapter
24. 【Android教程】适配器 Adapter
88 3
|
5月前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
148 2
|
6月前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
59 2
|
6月前
|
XML 编解码 Java
Android控件之高级控件——ListView、cardView、屏幕适配
Android控件之高级控件——ListView、cardView、屏幕适配
|
6月前
|
XML Java Android开发
Android Studio App开发之列表类视图中基本适配器BaseAdapter的使用及实战(附源码 超详细)
Android Studio App开发之列表类视图中基本适配器BaseAdapter的使用及实战(附源码 超详细)
203 0
|
11月前
|
XML Android开发 数据格式
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
75 0
|
Java Android开发
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo