Android ListView与适配器

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

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

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

Android ListView与适配器

布局:

<?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));
    }
}

上一篇:Android SlideMenu


下一篇:hdfs-site.xml配置