博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android ListView的使用
阅读量:7073 次
发布时间:2019-06-28

本文共 7336 字,大约阅读时间需要 24 分钟。

 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,如需转载请自行联系原作者

你可能感兴趣的文章
千兆级LTE的一小步,5G之路的一大步
查看>>
跟我一起写 Makefile(一)
查看>>
管理日志-原创理论工具--技能方格图
查看>>
MPLS TE第一步:创建基本TE隧道
查看>>
windows中禁止U盘写入
查看>>
Bash技巧总结
查看>>
在窗体中添加标签Label、Icon图标
查看>>
Perl脚本学习笔记(一)
查看>>
基于BIND实现DNS的解析、主从、子域、请求转发、访问控制
查看>>
Oracle Number用法
查看>>
nat
查看>>
基于Cisco技术的MPLS原理以及应用实现[一]
查看>>
iPhone/Mac Objective-C内存管理原理
查看>>
极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)
查看>>
Resin HTTPS 安装指南
查看>>
无法加入域
查看>>
在RHEL5下构建LAMP网站服务平台之架设Discuz!论坛
查看>>
.NET应用架构设计:原则、模式与实践 目录预览
查看>>
关于vector性能的测试(一)
查看>>
【移动开发】Android应用开发者应该知道的东西
查看>>