当前位置: 代码迷 >> Android >> android 开发之路-ListView 运用
  详细解决方案

android 开发之路-ListView 运用

热度:438   发布时间:2016-05-01 11:14:58.0
android 开发之路-------ListView 使用

        这张文章主要针对老道(csdn官方群)兄弟说上一博客SQLite的具体使用没写的很明白做补充。。非常感谢来捧场。你是第一个 偷笑

在这之前我犯了一个非常低级的错误。ListView 里面是不能添加控件的。只能后台引用其他的UI。希望新学的兄弟别犯同样的错误。

 

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@+id/lvSource"        android:layout_width="match_parent"        android:layout_height="313dp"        android:layout_x="0dp"        android:layout_y="0dp" /></AbsoluteLayout>

这是ListView_Demo.xml 代码 

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:orientation="horizontal"    android:layout_height="match_parent" >        <TextView        android:id="@+id/Uname"        android:layout_width="200dp"        android:layout_height="match_parent"         />    <TextView        android:id="@+id/Upwd"        android:layout_width="200dp"        android:layout_height="match_parent"         /></LinearLayout>


这是ListViewItem 的代码。。主要显示 listView 一行里有 一个name一个pwd 字段数据,下面主要讲解后台代码

         private ListView  list;	private SqlLiteCreateDateBase  sqlCreate;	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.listview_demo);		Log.e("ListView演示", "初始化成功。。。");		 list=(ListView)this.findViewById(R.id.lvSource);		 sqlCreate=new SqlLiteCreateDateBase(this) {		};		try {			Cursor  c=sqlCreate.getReadableDatabase().rawQuery("select username,userpwd from userinfo",null);			Log.e("ListView演示", "获取数据成功");			List<Map<String, Object>>  listSource=this.GetSource(c);			SimpleAdapter   sa=new SimpleAdapter(this,listSource, R.layout.listviewitem, 					new String[]{"userName","userPwd"}, new int[]{R.id.Uname,R.id.Upwd});			list.setAdapter(sa);		} catch (Exception e) {			new AlertDialog.Builder(null)			.setTitle("标题")			.setMessage("消息:"+e.getMessage())			.setPositiveButton("确认", null)			.show();		}


其实代码很简单 大家一看都明白 。最主要的就是封装解析Cursor  我把数据封装到List<Map<String, Object>> 对象里面 便于Adapter于ListView。这代码最主要的是讲解下SimpleAdapter ;android.widget.SimpleAdapter.SimpleAdapter(Context context, List<? extendsMap<String, ?>> data,int resource, String[] from,int[] to) 大家看下这个类。第二个参数主要是传数据集合,第三个参数主要穿 listView行对象。注意这个 一般情况都是一个单独的UI。千万别写到ListView里面。第四个是字段名。第五个是绑定UI上控件的名字。下面我贴出如何解析Cursor数据。

public List<Map<String, Object>> GetSource(Cursor cursor)	{		List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();		Map<String, Object>  m=null;//		while(cursor.moveToNext())//		{		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())		{			m=new HashMap<String, Object>();			m.put("userName","账号:"+ cursor.getString(cursor.getColumnIndex("userName")));			m.put("userPwd","密码:"+cursor.getString(cursor.getColumnIndex("userPwd")));			list.add(m);					}		Log.e("ListView演示", "数据解析成功");		Log.e("数据行数:", cursor.getCount()+"行");		return list;	}


Cursor主要有几个经常用的函数:moveToFirst(),moveToLast(),moveToNext(),moveToPosition(int position),moveToPrevious() 看字面意思大家估计也看的出来。第一个是表示光标移到第一行。最后一行、下一行、绝对定位某一行、上一行。等 具体的可去查看它的用法。都很简单。

使用 Android Cursor 的时候你必须先知道关于 Cursor 的几件事情:

使用 moveToFirst() 定位第一行。
你必须知道每一列的名称。
你必须知道每一列的数据类型。
Cursor 是一个随机的数据源。
所有的数据都是通过下标取得。 

      好了今天的ListView就讲解这些。。其实我学android主要是做产品的。主要是和硬件打交道串口通信之类的。可能是扫描条码。二维码 之类的设备。可能后期的博客会侧重于这方面。谢谢大家的关注。你的关注就是我的动力。。

 






 

 

4楼ruanwei19873天前 00:23
沙发,感谢分享。
3楼wuyazhe4天前 16:38
沙发,感谢分享。
2楼dengliren4天前 15:09
感谢分享
1楼haoquesen4天前 15:09
感 谢分享
  相关解决方案