标签:current tco http wrap idg line created tun contex
(一)
1.效果图
2.activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 4 android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" 5 android:paddingLeft="@dimen/activity_horizontal_margin" 6 android:paddingRight="@dimen/activity_horizontal_margin" 7 android:paddingTop="@dimen/activity_vertical_margin" 8 tools:context="com.example.app2.MainActivity"> 9 10 <ListView 11 android:id="@+id/lv" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" /> 14 </LinearLayout>
3.MianActivity.java
1 package com.example.app2; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.ListView; 6 7 import java.text.SimpleDateFormat; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 public class MainActivity extends AppCompatActivity { 12 private ListView listView; 13 private List<Student>list; 14 private Student student; 15 private MyAdapter myAdapter; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 listView = (ListView) findViewById(R.id.lv); 22 23 //新建数据源 24 list = new ArrayList<>(); 25 for (int i=0;i<20;i++){ 26 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 27 String time = simpleDateFormat.format(System.currentTimeMillis()); 28 student = new Student(); 29 student.setStuName("张三"+i); 30 student.setStuNum("201503"+i); 31 student.setTime(time); 32 list.add(student); 33 34 35 } 36 37 //新建适配器,适配器绑定数据 38 myAdapter = new MyAdapter(MainActivity.this,list); 39 40 //视图加载适配器 41 listView.setAdapter(myAdapter); 42 43 44 45 46 47 } 48 }
3.Studen.java(Alt+insert快捷键提示构造方法等)
1 package com.example.app2; 2 3 /** 4 * Created by Administrator on 2018/5/16. 5 */ 6 public class Student { 7 private String stuName; 8 private String stuNum; 9 private String time; 10 11 public String getStuNum() { 12 return stuNum; 13 } 14 15 public void setStuNum(String stuNum) { 16 this.stuNum = stuNum; 17 } 18 19 public String getStuName() { 20 return stuName; 21 } 22 23 public void setStuName(String stuName) { 24 this.stuName = stuName; 25 } 26 27 public String getTime() { 28 return time; 29 } 30 31 public void setTime(String time) { 32 this.time = time; 33 } 34 }
4.自定义适配器MyAdapter.java
1 package com.example.app2; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.TextureView; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.ImageView; 10 import android.widget.TextView; 11 12 import java.util.List; 13 14 /** 15 * Created by Administrator on 2018/5/16. 16 */ 17 public class MyAdapter extends BaseAdapter { 18 private Context context; 19 private List<Student> list; 20 21 public MyAdapter(Context context, List<Student> list) { 22 this.context = context; 23 this.list = list; 24 } 25 26 @Override 27 public int getCount() { 28 return list.size(); 29 } 30 31 @Override 32 public Object getItem(int position) { 33 return list.get(position); 34 } 35 36 @Override 37 public long getItemId(int position) { 38 return position; 39 } 40 41 @Override 42 public View getView(int position, View convertView, ViewGroup parent) { 43 convertView = LayoutInflater.from(context).inflate(R.layout.item,null); 44 // 获取控件 45 ImageView imageView = (ImageView)convertView.findViewById(R.id.iv); 46 TextView tv_name = (TextView)convertView.findViewById(R.id.tv_name); 47 TextView tv_num = (TextView)convertView.findViewById(R.id.tv_num); 48 TextView tv_time = (TextView)convertView.findViewById(R.id.tv_time); 49 50 Student student = list.get(position); 51 imageView.setImageResource(R.mipmap.ic_launcher); 52 tv_name.setText(student.getStuName()); 53 tv_num.setText(student.getStuNum()); 54 tv_time.setText(student.getTime()); 55 56 57 return convertView; 58 } 59 }
5.自定义item.xml布局
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal"> 6 <ImageView 7 android:id="@+id/iv" 8 android:background="@mipmap/ic_launcher" 9 android:layout_width="100dp" 10 android:layout_height="100dp" /> 11 <LinearLayout 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:orientation="vertical"> 15 <TextView 16 android:layout_marginTop="10dp" 17 android:layout_marginLeft="20dp" 18 android:textSize="20dp" 19 android:id="@+id/tv_name" 20 android:layout_width="100dp" 21 android:layout_height="50dp" /> 22 <TextView 23 android:layout_marginLeft="20dp" 24 android:textSize="20dp" 25 android:id="@+id/tv_num" 26 android:layout_width="100dp" 27 android:layout_height="30dp" /> 28 </LinearLayout> 29 <TextView 30 android:id="@+id/tv_time" 31 android:layout_marginTop="30dp" 32 android:textSize="20dp" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" /> 35 </LinearLayout>
另:优化后的MyAdapter.java
1 package com.example.app2; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.TextureView; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.ImageView; 10 import android.widget.TextView; 11 12 import java.util.List; 13 14 /** 15 * Created by Administrator on 2018/5/16. 16 */ 17 public class MyAdapter extends BaseAdapter { 18 private Context context; 19 private List<Student> list; 20 21 public MyAdapter(Context context, List<Student> list) { 22 this.context = context; 23 this.list = list; 24 } 25 26 @Override 27 public int getCount() { 28 return list.size(); 29 } 30 31 @Override 32 public Object getItem(int position) { 33 return list.get(position); 34 } 35 36 @Override 37 public long getItemId(int position) { 38 return position; 39 } 40 41 @Override 42 public View getView(int position, View convertView, ViewGroup parent) { 43 /* convertView = LayoutInflater.from(context).inflate(R.layout.item,null); 44 // 获取控件 45 ImageView imageView = (ImageView)convertView.findViewById(R.id.iv); 46 TextView tv_name = (TextView)convertView.findViewById(R.id.tv_name); 47 TextView tv_num = (TextView)convertView.findViewById(R.id.tv_num); 48 TextView tv_time = (TextView)convertView.findViewById(R.id.tv_time); 49 50 Student student = list.get(position); 51 imageView.setImageResource(R.mipmap.ic_launcher); 52 tv_name.setText(student.getStuName()); 53 tv_num.setText(student.getStuNum()); 54 tv_time.setText(student.getTime());*/ 55 ViewHolder viewHolder = null; 56 if(viewHolder==null){ 57 viewHolder = new ViewHolder(); 58 convertView = LayoutInflater.from(context).inflate(R.layout.item,null); 59 viewHolder.imageView = (ImageView)convertView.findViewById(R.id.iv); 60 viewHolder.tv_name = (TextView)convertView.findViewById(R.id.tv_name); 61 viewHolder.tv_num = (TextView)convertView.findViewById(R.id.tv_num); 62 viewHolder.tv_time = (TextView)convertView.findViewById(R.id.tv_time); 63 convertView.setTag(viewHolder); 64 65 }else { 66 viewHolder=(ViewHolder)convertView.getTag(); 67 } 68 Student student = list.get(position); 69 viewHolder.imageView.setImageResource(R.mipmap.ic_launcher); 70 viewHolder.tv_name.setText(student.getStuName()); 71 viewHolder.tv_num.setText(student.getStuNum()); 72 viewHolder.tv_time.setText(student.getTime()) ; 73 return convertView; 74 } 75 class ViewHolder{ 76 ImageView imageView; 77 TextView tv_name ; 78 TextView tv_num ; 79 TextView tv_time ; 80 } 81 }
标签:current tco http wrap idg line created tun contex
原文地址:https://www.cnblogs.com/sunxiaoyan/p/9044723.html