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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 前端必备技能之如何制作精灵图「建议收藏」

    前端必备技能之如何制作精灵图「建议收藏」为什么要用精灵图?比如京东首页的这些icon,如果每个icon都去请求一个资源。是非常浪费资源的。因为我们浏览器在同一个域名下并发加载的资源(CSS、JS、图片等)数量是有限的。我们可以将这些icon放到一张图片,通过截取这张图片的不同的区域,拿到不同的icon制作一个背景透明的Icon截图你喜欢的logo,最好是正方形。打开ps,使用快速选择工具选择白色区域ctrl+x删除…

    2022年6月3日
    126
  • forkjoin使用_forkjoin与线程池区别

    forkjoin使用_forkjoin与线程池区别ForkJoinPoolinfoForkJoinPool=newForkJoinPool(Runtime.getRuntime().availableProcessors()*2);ForkJoinTask<Map<Long,InfoVO>>forkJoinTask=ThreadPoolManage.infoForkJoinPool.submit(newPriceTask(skuIds,0,skuIds.size(),infoSoaService));

    2022年9月20日
    0
  • myeclipse10万能注册码_数码大师免费注册码

    myeclipse10万能注册码_数码大师免费注册码下面提供了100个MyEclipse6.5的注册码供大家使用:

    2022年9月30日
    0
  • yuv444 yuv420_硬盘转速和缓存哪个重要

    yuv444 yuv420_硬盘转速和缓存哪个重要YUV420与YUV444互转,YUV420与YUV444读取和保存,YUV的显示和播放功能【尊重原创,转载请注明出处】:https://blog.csdn.net/guyuealian/article/details/82454945  OpenCV提供了RGB与YUV420/YUV444互转的接口:cvtColor(),但根尴尬OpenCV就是没有提供YUV444与YUV420互转…

    2022年10月27日
    0
  • mysql数据库命令大全菜鸟_mysql常用命令[通俗易懂]

    mysql数据库命令大全菜鸟_mysql常用命令[通俗易懂]连接:mysql-h主机地址-u用户名-p用户密码(注:u与root可以不用加空格,其它也一样)创建授权:grantselecton数据库.*to用户名@登录主机identifiedby\”密码\”修改密码:mysqladmin-u用户名-p旧密码password新密码删除授权:revokeselect,insert,update,deleteom*.*f…

    2022年6月17日
    32
  • java是哪个公司的

    java一开始是Sun Microsystems公司开发的,但是在​2009年4月20日甲骨文(ORACLE)以现金收购Sun微系统公司,所以现在java属于甲骨文公司。

    2022年1月16日
    62

发表回复

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

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