Androidlistview_android listview的用法

Androidlistview_android listview的用法下面是activity:[java] viewplaincopypublic class MainActivity extends Activity {        private ListView mListView = null;      private List mList = null;        @Overri

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

下面是activity:

[java] 
view plain
copy

  1. public class MainActivity extends Activity {  
  2.   
  3.     private ListView mListView = null;  
  4.     private List<TestDate> mList = null;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         mListView = (ListView) this.findViewById(R.id.main_listView);  
  11.         mList = new ArrayList<TestDate>();  
  12.         initData();  
  13.         Collections.sort(mList, new Comparator<TestDate>() {  
  14.             /** 
  15.              *  
  16.              * @param lhs 
  17.              * @param rhs 
  18.              * @return an integer < 0 if lhs is less than rhs, 0 if they are 
  19.              *         equal, and > 0 if lhs is greater than rhs,比较数据大小时,这里比的是时间 
  20.              */  
  21.             @Override  
  22.             public int compare(TestDate lhs, TestDate rhs) {  
  23.                 Date date1 = DateUtil.stringToDate(lhs.getDate());  
  24.                 Date date2 = DateUtil.stringToDate(rhs.getDate());  
  25.                 // 对日期字段进行升序,如果欲降序可采用after方法  
  26.                 if (date1.before(date2)) {  
  27.                     return 1;  
  28.                 }  
  29.                 return –1;  
  30.             }  
  31.         });  
  32.         mListView.setAdapter(new MyAdapter(this, mList));  
  33.     }  
  34.   
  35.     private void initData() {  
  36.         mList.add(new TestDate(“2012-12-12 12:30”“zhangsan”));  
  37.         mList.add(new TestDate(“2012-12-12 10:20”“lisi”));  
  38.         mList.add(new TestDate(“2012-12-11 10:21”“lisi”));  
  39.         mList.add(new TestDate(“2012-12-11 10:20”“lisi”));  
  40.         mList.add(new TestDate(“2012-12-13 01:03”“wangwu”));  
  41.         mList.add(new TestDate(“2012-12-10 02:04”“zhaoliu”));  
  42.         mList.add(new TestDate(“2012-12-15 23:00”“tianqi”));  
  43.         mList.add(new TestDate(“2012-11-12 22:30”“wangwu”));  
  44.         mList.add(new TestDate(“2012-12-17 08:24”“shimei”));  
  45.         mList.add(new TestDate(“2012-11-10 11:10”“shisanmei”));  
  46.         mList.add(new TestDate(“2012-12-18 16:50”“wangan”));  
  47.         mList.add(new TestDate(“2012-12-19 18:00”“wangjiu”));  
  48.         mList.add(new TestDate(“2012-12-20 19:30”“wusi”));  
  49.         mList.add(new TestDate(“2012-12-20 19:30”“wusi”));  
  50.     }  
  51. }  

下面是工具类:

[java] 
view plain
copy

  1. public class DateUtil {  
  2.   
  3.     public static Date stringToDate(String dateString) {  
  4.         ParsePosition position = new ParsePosition(0);  
  5.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm”);  
  6.         Date dateValue = simpleDateFormat.parse(dateString, position);  
  7.         return dateValue;  
  8.     }  
  9.   
  10. }  

下面是ListView用的Adapter:

[java] 
view plain
copy

  1. public class MyAdapter extends BaseAdapter {  
  2.   
  3.     private Context mContext;  
  4.     private List<TestDate> mList;  
  5.   
  6.     public MyAdapter(Context context, List<TestDate> list) {  
  7.         this.mContext = context;  
  8.         this.mList = list;  
  9.     }  
  10.   
  11.     @Override  
  12.     public int getCount() {  
  13.         return mList != null ? mList.size() : 0;  
  14.     }  
  15.   
  16.     @Override  
  17.     public Object getItem(int position) {  
  18.         return mList.get(position);  
  19.     }  
  20.   
  21.     @Override  
  22.     public long getItemId(int position) {  
  23.         return position;  
  24.     }  
  25.   
  26.     @Override  
  27.     public View getView(int position, View convertView, ViewGroup parent) {  
  28.         ViewHolder holder = null;  
  29.         if (convertView == null) {  
  30.             convertView = (LinearLayout) LayoutInflater.from(mContext).inflate(  
  31.                     R.layout.main_item, null);  
  32.             holder = new ViewHolder();  
  33.             holder.textView1 = (TextView) convertView  
  34.                     .findViewById(R.id.item_textView1);  
  35.             holder.textVeiw2 = (TextView) convertView  
  36.                     .findViewById(R.id.item_textView2);  
  37.             convertView.setTag(holder);  
  38.         } else {  
  39.             holder = (ViewHolder) convertView.getTag();  
  40.         }  
  41.   
  42.         holder.textView1.setText(mList.get(position).getDate());  
  43.         holder.textVeiw2.setText(mList.get(position).getName());  
  44.   
  45.         return convertView;  
  46.     }  
  47.   
  48.     private class ViewHolder {  
  49.         private TextView textView1;  
  50.         private TextView textVeiw2;  
  51.     }  
  52.   
  53. }  

下面是xml文件:

[html] 
view plain
copy

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent” >  
  5.   
  6.     <ListView  
  7.         android:id=“@+id/main_listView”  
  8.         android:layout_width=“match_parent”  
  9.         android:layout_height=“match_parent”  
  10.         android:layout_centerHorizontal=“true”  
  11.         android:layout_centerVertical=“true”  
  12.         tools:context=“.MainActivity” />  
  13.   
  14. </RelativeLayout>  

 

[html] 
view plain
copy

  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.   
  7.     <TextView  
  8.         android:id=“@+id/item_textView1”  
  9.         android:layout_width=“wrap_content”  
  10.         android:layout_height=“wrap_content”  
  11.         android:layout_gravity=“center_vertical”  
  12.         android:layout_margin=“10dp” />  
  13.   
  14.     <TextView  
  15.         android:id=“@+id/item_textView2”  
  16.         android:layout_width=“wrap_content”  
  17.         android:layout_height=“wrap_content”  
  18.         android:layout_gravity=“center_vertical” />  
  19.   
  20. </LinearLayout>  

下面是一个JavaBean的类:

[java] 
view plain
copy

  1. public class TestDate {  
  2.       
  3.     private String date;  
  4.     private String name;  
  5.   
  6.     public String getDate() {  
  7.         return date;  
  8.     }  
  9.   
  10.     public String getName() {  
  11.         return name;  
  12.     }  
  13.   
  14.     public TestDate(String date, String name) {  
  15.         this.date = date;  
  16.         this.name = name;  
  17.     }  
  18.   
  19. }  
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186362.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vmware16安装centos8_虚拟机centos6安装教程

    vmware16安装centos8_虚拟机centos6安装教程VMware12安装centOS8(vm虚拟机安装centos8教程)前几天Centos8发布了,尽管他是8的第一个版本,有着许多的bug那么今天我们就在VM12上面安装centOS8吧,8这个图形化界面我个人感觉有点丑首先下载iso文件百度下点击进入官网点击马上获得centos然后选择这个选择离你近的镜像地址,点击下载打开vm12点击新建虚拟机点击下一步,如下图这样…

    2022年10月1日
    4
  • SqlSessionTemplate是如何保证MyBatis中SqlSession的线程安全的?「建议收藏」

    SqlSessionTemplate是如何保证MyBatis中SqlSession的线程安全的?「建议收藏」一、DefaultSqlSession的线程不安全性在MyBatis架构中SqlSession是提供给外层调用的顶层接口,实现类有:DefaultSqlSession、SqlSessionManager以及mybatis-spring提供的实现SqlSessionTemplate。默认的实现类为DefaultSqlSession如。类图结构如下所示:对于MyBatis提供的原生实现类来…

    2022年5月31日
    36
  • C++的三种单例模式—–深度解析

    C++的三种单例模式—–深度解析简介因为在设计或开发中,肯定会有这么一种情况,一个类只能有一个对象被创建,如果有多个对象的话,可能会导致状态的混乱和不一致。这种情况下,单例模式是最恰当的解决办法。它有很多种实现方式,各自的特性不相同,使用的情…

    2022年5月27日
    39
  • 全网最全Python项目体系练习500例(附源代码),练完可就业

    全网最全Python项目体系练习500例(附源代码),练完可就业个人公众号yk坤帝后台回复项目四获取整理资源1.有一个jsonline格式的文件file.txt大小约为10K2.补充缺失的代码3.输入日期,判断这一天是这一年的第几天?4.打乱一个排好序的list对象alist?5.现有字典d={‘a’:24,‘g’:52,‘i’:12,‘k’:33}请按value值进行排序?6.字典推导式7.请反转字符串“aStr”?8.将字符串“k:1|k1:2|k2:3|k3:4”,处理成字典{k:1,k1:2,…}9.请按alist中元

    2022年5月16日
    86
  • 分布式搜索elasticsearch 文献检索索引 入门

    分布式搜索elasticsearch 文献检索索引 入门

    2022年1月2日
    55
  • LocalDateTime方式字符串转Date

    LocalDateTime方式字符串转Date2019独角兽企业重金招聘Python工程师标准>>>…

    2022年10月3日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号