Android ListView的使用

简介:

 1、定义简单的适配器形式。

首先定义一个Layout为listviewitem.xml. 里面有三个TextView。分别代表学号,姓名,班级。

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:id= "@+id/cuslistViewItem"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "horizontal"  >
 
     <TextView
         android:id= "@+id/textView1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "TextView"
         android:layout_marginLeft= "10dp"
         android:layout_marginBottom= "10dp" />
 
     <TextView
         android:id= "@+id/textView3"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "TextView"
         android:layout_marginLeft= "10dp"
         android:layout_marginBottom= "10dp" />
 
     <TextView
         android:id= "@+id/textView2"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "TextView"
         android:layout_marginLeft= "10dp"
         android:layout_marginBottom= "10dp"  />
</LinearLayout>

 在activity_main.xml中放一个ListView控件,很简单,不解释。

<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:paddingBottom= "@dimen/activity_vertical_margin"
     android:paddingLeft= "@dimen/activity_horizontal_margin"
     android:paddingRight= "@dimen/activity_horizontal_margin"
     android:paddingTop= "@dimen/activity_vertical_margin"
     tools:context= ".MainActivity"  >
 
     <TextView
         android:id= "@+id/textView1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "@string/hello_world"  />
 
     <ListView
         android:id= "@+id/listView1"
         android:layout_width= "match_parent"
         android:layout_height= "wrap_content"
         android:layout_alignParentLeft= "true"
         android:layout_below= "@+id/textView1"
         android:layout_marginTop= "50dp"  >
     </ListView>
 
</RelativeLayout>

 

然后建立数据源

public  class  MainActivity extends  Activity {
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         ListView lv = (ListView) this .findViewById(R.id.listView1);
         
         ArrayList<HashMap<String,String>> listSource = new  ArrayList<HashMap<String,String>>();
         for ( int  num= 1 ; num< 10 ; num++)
         {
             HashMap<String,String> map = new  HashMap<String, String>();
             map.put( "num" ,String.valueOf(num));
             map.put( "name" , "stu" +num );
             map.put( "className" , "software 2" );
             listSource.add(map);
         }
         
         SimpleAdapter mStuInfo = new  SimpleAdapter(
                 this ,listSource,R.layout.listviewitem,
                 new  String[] { "num" , "name" , "className" },
                 new  int []{R.id.textView1, R.id.textView2, R.id.textView3});
         
         lv.setAdapter(mStuInfo);
     }
 
}

 最后将listview绑定适配器的数据源。效果图如下图。

 当然可以加入ItemClick事件。

lv.setOnItemClickListener( new  ItemClickListener());

然后实现ItemClickListener。

class  ItemClickListener implements  OnItemClickListener {
      //arg2 is the position
      //arg3 is row number
     @Override
     public  void  onItemClick(AdapterView<?> arg0, View arg1, int  arg2, long  arg3) {
         // TODO Auto-generated method stub
         HashMap<String, String> item=(HashMap<String, String>) arg0.getItemAtPosition(arg2);
         Toast.makeText(MainActivity. this , item.get( "name" ), Toast.LENGTH_LONG).show();
     }
 
}

 ListView和GridView在此用法是一样的。可以把ListView控件换成GridView控件。

 

二、下面采用新的适配器形式。将添加学生图片,并重新组织布局。效果如下图:

代码如下:

重新设计的listviewitem.xml.

<?xml version= "1.0"  encoding= "utf-8" ?>
<RelativeLayout    xmlns:android= "http://schemas.android.com/apk/res/android"
     android:id= "@+id/cuslistViewItem"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "horizontal"  >
 
     <ImageView
         android:id= "@+id/imageView1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:layout_marginLeft= "10dp"
         android:layout_marginBottom= "10dp"  />
 
     <TextView
         android:id= "@+id/textView1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "TextView"
         android:layout_toRightOf= "@+id/imageView1"
         android:layout_marginLeft= "10dp"
         android:layout_marginBottom= "10dp" />
 
     <TextView
         android:id= "@+id/textView2"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "TextView"
         android:layout_toRightOf= "@+id/textView1"
         android:layout_marginLeft= "10dp"
         android:layout_marginBottom= "10dp" />
 
     <TextView
         android:id= "@+id/textView3"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "TextView"
         android:layout_toRightOf= "@+id/imageView1"
         android:layout_below= "@+id/textView1"
         android:layout_marginLeft= "10dp"
         android:layout_marginBottom= "30dp"  />
</RelativeLayout>

 

重新的数据源。建立了MyListViewAdapter类继承BaseAdapter。

public  class  MainActivity extends  Activity {
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         ListView lv = (ListView) this .findViewById(R.id.listView1);
         
         ArrayList<HashMap<String,String>> listSource = new  ArrayList<HashMap<String,String>>();
         for ( int  num= 1 ; num< 10 ; num++)
         {
             HashMap<String,String> map = new  HashMap<String, String>();
             map.put( "photo" , String.valueOf(R.drawable.ic_launcher));
             map.put( "num" ,String.valueOf(num));
             map.put( "name" , "stu" +num );
             map.put( "className" , "software 2" );
             listSource.add(map);
         }
         lv.setAdapter( new  MyListViewAdapter(listSource));
     }
     
     public  class  MyListViewAdapter extends  BaseAdapter{
 
         View[] itemViews;
         public  MyListViewAdapter(ArrayList<HashMap<String,String>> listSource)
         {
             itemViews = new  View[listSource.size()];
             Iterator<HashMap<String, String>> it = listSource.iterator();
             HashMap<String,String> hash;
             int  i = 0 ;
             try {
                 while (it.hasNext())
                 {
                     hash = it.next();
                     itemViews[i] =  makeItemView(hash);
                     i++;
                 }
             } catch (Exception e){
                 Log.e( "Error" , e.getMessage());
                 e.printStackTrace();
             }
     
         }
         
         private  View makeItemView(HashMap<String,String> map) { 
             
                         if (map == null  || map.isEmpty()) return  null ;
                         LayoutInflater inflater = (LayoutInflater) MainActivity. this 
                               .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                         
                         // 使用View的对象itemView与R.layout.item关联  
                         View itemView =  inflater.inflate(R.layout.listviewitem, null ); 
               
                         TextView title = (TextView) itemView.findViewById(R.id.textView1); 
                         title.setText(map.get( "num" )); 
                         Log.e( "num"  , map.get( "num" ));
                         
                         TextView text = (TextView) itemView.findViewById(R.id.textView2); 
                         text.setText(map.get( "name" ));
                         Log.e( "name" , map.get( "name" ));
                         
                         TextView text2 = (TextView) itemView.findViewById(R.id.textView3); 
                         text2.setText(map.get( "className" ));
                         Log.e( "className" , map.get( "className" ));
                         
                         ImageView image = (ImageView) itemView.findViewById(R.id.imageView1); 
                         image.setImageResource(Integer.parseInt(map.get( "photo" )));
                         Log.e( "photo" , map.get( "photo" ));
                           
                         return  itemView; 
                    }
 
         
         @Override
         public  int  getCount() {
             // TODO Auto-generated method stub
             return  itemViews.length;
         }
 
         @Override
         public  Object getItem( int  position) {
             // TODO Auto-generated method stub
             return  itemViews[position];
         }
 
         @Override
         public  long  getItemId( int  position) {
             // TODO Auto-generated method stub
             return  position;
         }
 
         @Override
         public  View getView( int  position, View convertView, ViewGroup parent) {
             // TODO Auto-generated method stub
             if (convertView == null )
                 return  itemViews[position];
             return  convertView;
         }}
 
         }
 
}

 



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/14/2960192.html,如需转载请自行联系原作者

目录
相关文章
|
9月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
120 2
|
9月前
|
XML Java Android开发
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
812 1
|
8月前
|
API Android开发 开发者
`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView
【6月更文挑战第26天】`RecyclerView`是Android API 21引入的UI组件,用于替代ListView和GridView。它提供高效的数据视图复用,优化的布局管理,支持多种布局(如线性、网格),并解耦数据、适配器和视图。RecyclerView的灵活性、性能(如局部刷新和动画支持)和扩展性使其成为现代Android开发的首选,特别是在处理大规模数据集时。
104 2
|
8月前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
291 2
|
9月前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
89 2
|
9月前
|
XML 编解码 Java
Android控件之高级控件——ListView、cardView、屏幕适配
Android控件之高级控件——ListView、cardView、屏幕适配
101 0
|
XML Android开发 数据格式
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
96 0
|
Java Android开发
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
|
XML Java Android开发
Android优化版ListView(附源代码)
本文是博主对Adapter(适配器)的一些理解,为了加深对Adapter的理解以及记录自己的阶段学习而写,同时也适合初学者阅读,参考本条博客的逻辑进行学习。
159 0
|
Android开发
安卓中listview点击每一条进入不同界面
安卓中listview点击每一条进入不同界面
121 0

热门文章

最新文章

  • 1
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 2
    Android历史版本与APK文件结构
  • 3
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 4
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 5
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 6
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 7
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 9
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 10
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
  • 1
    android FragmentManager 删除所有Fragment 重建
    15
  • 2
    Android实战经验之Kotlin中快速实现MVI架构
    21
  • 3
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
    24
  • 4
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    39
  • 5
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    122
  • 6
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    40
  • 7
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 8
    Android历史版本与APK文件结构
    149
  • 9
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40