码迷,mamicode.com
首页 > 其他好文 > 详细

赵雅智_ListView_SimpleAdapter

时间:2014-06-10 10:55:23      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:android   c   class   blog   code   java   

项目步骤

  1. 声明listView控件并获取显示的视图
  2. 获取显示的数据
  3. 设置显示的adapter
  4. 注册点击事件

具体案例

实现效果:
bubuko.com,布布扣

查找的方法

public List<Map<String, Object>> getData() {
		SQLiteDatabase db = dh.getWritableDatabase();
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		Cursor c = db
				.rawQuery(
						"select userid ,username,userage,usersalary,userphone from users",
						null);

		while (c.moveToNext()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", c.getInt(c.getColumnIndex("userid")));
			map.put("name", c.getString(c.getColumnIndex("username")));
			map.put("age", c.getInt(c.getColumnIndex("userage")));
			map.put("phone", c.getString(c.getColumnIndex("userphone")));
			data.add(map);
		}
		db.close();
		return data;
	}


布局文件

同BaseAdapter的布局文件activity_main.xml

ArrayAdapter实现类(主程序)


package com.example.android_sqlite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import com.example.android_sqlite.dao.impl.UserDaoImpls;
import com.example.android_sqlite.database.DatabaseHelper;

/**
 * SimpleAdapter
 * 
 * @author zhaoyazhi
 * 
 */
public class MainActivity extends Activity implements OnItemClickListener {
	private ListView lv_users;// 视图层

	// model层 相关的对象的声明
	private DatabaseHelper dh;
	private UserDaoImpls userDao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 模型层
		dh = new DatabaseHelper(this);// 创建数据库
		userDao = new UserDaoImpls(dh);// 获取显示数据 model
		List<Map<String, Object>> data = userDao.getData();// 查询获取数据

		// 控制层 依赖于模型层
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.item_phone, new String[] { "name", "phone", "id",
						"age" }, new int[] { R.id.tv_name, R.id.tv_phone,
						R.id.tv_id, R.id.tv_age });

		// 视图层 依赖于控制层
		lv_users = (ListView) findViewById(R.id.lv_users);
		lv_users.setAdapter(adapter);

		// 注册点击事件
		lv_users.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long arg3) {

		// 获取点击我的数据
		Toast.makeText(this, parent.getItemAtPosition(position) + "", 0).show();

	}

}


赵雅智_ListView_SimpleAdapter,布布扣,bubuko.com

赵雅智_ListView_SimpleAdapter

标签:android   c   class   blog   code   java   

原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/29791705

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!