项目步骤
- 声明listView控件并获取显示的视图
- 获取显示的数据
- 设置显示的adapter
- 注冊点击事件
详细案例
实现效果:
查找的方法
public List
布局文件
同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> 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(); }}