• 赵雅智_ListView_SimpleAdapter


    项目步骤

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

    详细案例

    实现效果:

    查找的方法

    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();
    
    	}
    
    }
    


  • 相关阅读:
    The differences between genetic distance values of Nei 1978 and Nei 1972
    【打油诗】无题
    EXCEL函数LookUp, VLOOKUP及HLOOKUP函数
    【菜鸟入门R语言】独立t检验
    【菜鸟入门R语言】利用R语言画箱线图
    【综述】植物防御假说——Out of the quagmire of plant defense hypotheses
    一个创造奇迹的上午——谈如何高效利用时间
    两篇波兰地区重金属污染地区土壤微生物多样性的研究
    【软件使用】利用image J测定叶面积
    利用Peakscanner软件对测序仪获取的分子标记数据进行初步识别
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6919737.html
Copyright © 2020-2023  润新知