android——ListView功能的实现
1、main.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="vertical" > <ListView android:id="@+id/personLV"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </LinearLayout>
2、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" > <TextView android:id="@+id/idTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:textSize="20sp"
/> <TextView android:id="@+id/nameTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:padding="10dp"
android:textSize="20sp"
/> <TextView android:id="@+id/balanceTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:padding="10dp"
android:textSize="20sp"
/> </LinearLayout>
3、MainActivity
package com.njupt.listview; import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity { private ListView personLV;
private List<Person> persons; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); personLV = (ListView) findViewById(R.id.personLV); PersonDao dao = new PersonDao(this); persons = dao.queryAll();
personLV.setAdapter(new MyBaseAdapter());//给ListView添加adapter,按照adapter中的方法给ListView添加条目
} private class MyBaseAdapter extends BaseAdapter{// 定义Adapter, 把每个Person对象生成一个条目, 将所有条目装入ListView @Override
public int getCount() {//获取条目数量
return persons.size();
} @Override
public Object getItem(int position) {//获取指定对象
return persons.get(position);
} @Override
public long getItemId(int position) {//获取指定对象的id
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {// 返回指定位置上的条目, 条目会被自动添加到ListView中 View item = View.inflate(getApplicationContext(), R.layout.item, null);// 根据布局文件创建View(LinearLayout) TextView idTV = (TextView) item.findViewById(R.id.idTV);// 获取这个新生成的View中的TextView
TextView nameTV = (TextView) item.findViewById(R.id.nameTV);
TextView balanceTV = (TextView) item.findViewById(R.id.balanceTV); Person p = persons.get(position);// 根据位置获取Person对象
idTV.setText(p.getId() + "");// 给TextView设置文本
nameTV.setText(p.getName());
balanceTV.setText(p.getBalance()+""); return item;
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
-------------------------------------------------------
结果如下所示:
android——ListView功能的实现的更多相关文章
- android listView功能简介
本文参考连接:http://blog.csdn.net/kesenhoo/article/details/7196920 android中listView是非常常用的组建,下边就经常用到的功能做一下简 ...
- Android ListView异步载入图片乱序问题,原因分析及解决方式
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/45586553 在Android全部系统自带的控件其中,ListView这个控件算是 ...
- 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...
- Android ListView 常用技巧
Android ListView 常用技巧 Android TextView 常用技巧 1.使用ViewHolder提高效率 ViewHolder模式充分利用了ListView的视图缓存机制,避免了每 ...
- Android ListView滑动过程中图片显示重复错乱闪烁问题解决
最新内容建议直接访问原文:Android ListView滑动过程中图片显示重复错乱闪烁问题解决 主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及L ...
- android ListView优化
android ListView通过优化重用历史缓存实现.listview相应的数据适配器一般使用自己定义BaseAdapter子类,重用历史缓冲区来提高性能. 例如,下面的示例代码演示: 1.lis ...
- Android ListView分页载入(服务端+android端)Demo
Android ListView分页载入功能 在实际开发中经经常使用到,是每一个开发人员必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了測试. 服务端使用MyEcli ...
- [问题]Android listView item edittext 不能调用软键盘输入法
android listview item edittext not softkeyboard edittext可以获取焦点, 可以触发事件, 但是就是不能调用输入法, 不知道为什么? 难道不能在i ...
- android listview中item通过viewpager实现《IT蓝豹》
android listview中item通过viewpager实现 android listview中item通过viewpager实现,每一个item都支持viewpager实现图片切换功能.本项 ...
随机推荐
- HDU 2393 Higher Math
#include <cstdio> #include <string> using namespace std; void swap(int& a,int& b ...
- 使用Lock实现信号量
public class SemaphoreOnLock { private final Lock lock = new ReentrantLock(); private fi ...
- 谁动了我的CurrentPrincipal?解释一下为什么CurrentPrincipal变了,并解决这个问题。
在上一篇博客中我遇到了一个问题,并且导致了我无法继续进行授权和验证.经过查阅资料和解决另外一个问题的过程,我突然想通了为什么CurrentPrincipal变了.并且经过验证后的确是我所理解的那样.下 ...
- VisualSVN Server安装后,TortoiseSVN远程无法访问版本库。
修正!重演了一遍,发现总结有误,重新整理下.首先访问版本库的路径不清楚的话可以在VisualSVN Server的版本库上右键“Copy URL to Clipboard”.访问版本库失败的几种情况: ...
- Elasticsearch 1.4.X 的CORS设置
最近将Elasticsearch由1.2.2升级到1.4.4后,sense工具无法使用了,它发到Elasticsearch集群的请求没有任何的响应.阅读了Elasticsearch的文档才了解到,这是 ...
- html基本框架
- python优秀库 - 使用xmltodict解析xml文档
上次讲到如何使用BeautifulSoup解析XML文档,今天发现另外一个python库xmltodict(https://github.com/martinblech/xmltodict)也很简单. ...
- VS QT 配置OpenGL
在visual studio 下编译OpenGL代码出现以下错误,原因是vs没有自带opengl库,需要自己引入 无法解析的外部符号 __imp__glClear@4 无法解析的外部符号 __imp_ ...
- jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL
这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...
- poj1742 多重背包的可行性问题
http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ...