简单的android Listview使用例子
来源:互联网 发布:安卓一键清除软件数据 编辑:程序博客网 时间:2024/04/28 05:54
为了熟悉Listview的使用,做了一个小例子联系一下,
主要步骤:
1. 在MainActivity中,创建一个adapter对象(可以是android自带的ArrayAdapter,也可以是自定义的如SongAdapter)
2. 如果自定义,就要创建ListView的子项,如song_listview_item.xml
3. 创建ListView对象,并用setAdapter方法把adapter装进去
4. 用setOnItemClickListener设置点击事件
需要注意的是,在自定义的SongAdapter中最好能用convertView参数和一个缓存控件实例的类对象如ViewHolder来优化ListView的运行效率。
具体见源码
/*SongAdapter*/public class SongAdapter extends ArrayAdapter<SongInfo> { private int resourceId; public SongAdapter(Context context, int resourceId, List<SongInfo> objects) { super(context, resourceId, objects); this.resourceId = resourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; ViewHolder holder; SongInfo songInfo = getItem(position); if (null == convertView) { view = LayoutInflater.from(getContext()).inflate(resourceId, null); holder = new ViewHolder(); holder.tvSongName = (TextView)view.findViewById(R.id.tvSongName); holder.tvSinger = (TextView)view.findViewById(R.id.tvSinger); view.setTag(holder); }else { view=convertView; holder = (ViewHolder)view.getTag(); }// TextView tvSongName = (TextView)view.findViewById(R.id.tvSongName);// TextView tvSinger = (TextView)view.findViewById(R.id.tvSinger); holder.tvSongName.setText(songInfo.getSongName()); holder.tvSinger.setText(songInfo.getSinger()); return view; } //缓存子布局中的控件实例 class ViewHolder{ TextView tvSongName; TextView tvSinger; }}
SongInfo.java
public class SongInfo { private String songName; private String singer; public SongInfo(String songName, String singer) { this.songName = songName; this.singer = singer; } public String getSongName() { return songName; } public String getSinger() { return singer; }}
MainActivity.java
public class MainActivity extends Activity { private String[] songStrings = { "冰雨 刘德华", "东邪西毒 张学友", "三天三夜 张惠妹", "齐天大圣 张卫健", "光辉岁月 Beyond", "两只蝴蝶 庞龙", "冰雨 刘德华", "东邪西毒 张学友", "三天三夜 张惠妹", "齐天大圣 张卫健", "光辉岁月 Beyond", "两只蝴蝶 庞龙", "冰雨 刘德华", "东邪西毒 张学友", "三天三夜 张惠妹", "齐天大圣 张卫健", "光辉岁月 Beyond", "两只蝴蝶 庞龙" }; private List<SongInfo> songList = new ArrayList<SongInfo>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// ArrayAdapter<String> adapter = new ArrayAdapter<String>(// MainActivity.this, R.layout.song_listview_item,// songStrings); initSongList(); SongAdapter songAdapter = new SongAdapter(MainActivity.this, R.layout.song_listview_item, songList); ListView lvListView=(ListView)findViewById(R.id.lv); lvListView.setAdapter(songAdapter); lvListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub SongInfo song=songList.get(position); Toast.makeText(MainActivity.this, song.getSongName(), Toast.LENGTH_LONG).show(); } }); } public void initSongList() { SongInfo song = new SongInfo("冰雨", "刘德华"); songList.add(song); SongInfo song1 = new SongInfo("东邪西毒", "张学友"); songList.add(song1); SongInfo song2 = new SongInfo("三天三夜", "张惠妹"); songList.add(song2); SongInfo song3 = new SongInfo("齐天大圣", "张卫健"); songList.add(song3); SongInfo song4 = new SongInfo("光辉岁月", "Beyond"); songList.add(song4); SongInfo song5 = new SongInfo("两只蝴蝶", "庞龙"); songList.add(song5); SongInfo song6 = new SongInfo("冰雨", "刘德华"); songList.add(song6); SongInfo song7 = new SongInfo("东邪西毒", "张学友"); songList.add(song7); SongInfo song8 = new SongInfo("三天三夜", "张惠妹"); songList.add(song8); SongInfo song9 = new SongInfo("齐天大圣", "张卫健"); songList.add(song9); SongInfo song10 = new SongInfo("光辉岁月", "Beyond"); songList.add(song10); SongInfo song11 = new SongInfo("两只蝴蝶", "庞龙"); songList.add(song11); SongInfo song12 = new SongInfo("冰雨", "刘德华"); songList.add(song12); SongInfo song13 = new SongInfo("东邪西毒", "张学友"); songList.add(song13); SongInfo song14 = new SongInfo("三天三夜", "张惠妹"); songList.add(song14); SongInfo song15 = new SongInfo("齐天大圣", "张卫健"); songList.add(song15); SongInfo song16 = new SongInfo("光辉岁月", "Beyond"); songList.add(song16); SongInfo song17 = new SongInfo("两只蝴蝶", "庞龙"); songList.add(song17); }}
接下来两个XML文件
主文件activity_main.xml
<LinearLayout 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:orientation="vertical" 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="com.starnet.mediaplayer2017.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#FFF0F5" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="小纯纯" /> </LinearLayout> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout>
listview的子项XML,song_listview_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/tvSongName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/tvSinger" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dp" /> </LinearLayout></LinearLayout>
0 0
- 简单的android Listview使用例子
- Android ListView 简单例子
- Android ListView的简单使用
- Android ListView的简单使用
- android中ListView的使用例子
- android中控件ListView的简单实用例子
- VB.NET 中 使用 ListView 控件的简单例子
- VB.NET 中 使用 ListView 控件的简单例子
- VB.NET中使用ListView控件的简单例子
- Android ListView简单使用
- Android ListView 简单使用
- Android ListView 列表控件的简单使用
- Android ListView 列表控件的简单使用
- android中ListView的简单使用
- Android初试--ListView的简单使用
- Android中ListView的简单使用
- Android常用控件--ListView的简单使用
- android ListView的简单使用与优化
- The Knowledge of Linux-.-Day04
- 如何在wine下为Source Insight配置字体
- Java并发编程的艺术(五)——中断
- (HDU 5738) <计算几何 ,在n个点中找所有共线的集合> 2016 Multi-University Training Contest 2
- Java 基础知识积累 时间Date那点事
- 简单的android Listview使用例子
- java求整数的位数
- Java设计模式——工厂设计模式
- MongoDB 导出导入备份恢复数据详解及实例
- 织梦建站的时候文章或者图集自增办法
- Spring mvc HelloWorld
- odoo系统中如何配置收发邮件服务器?
- 软件测试中常用的SQL语句(初级篇)
- (转载)理解内存管理的本质