zoukankan      html  css  js  c++  java
  • ListView用SimpleAdapter适配器绑定

    
    

      1、在ListView里显示数据

    private void show() {
    		List<Person> persons=service.getScrollData(0, 20);//需要绑定的数据,getScrollData代码在下方
    List<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>(); for(Person person:persons){ HashMap<String, Object> item=new HashMap<String, Object>(); item.put("name", person.getName()); item.put("id", person.getId()); item.put("phone", person.getPhone()); item.put("amount", person.getAmount()); data.add(item); } SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), data, R.layout.item, new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount}); listView.setAdapter(adapter); }
    

      2、getScrollData代码

    	/**
    	 * 分页获取记录
    	 * @param offset 跳过前面多少条记录
    	 * @param maxResult 每页显示多少条记录
    	 * @return
    	 */
    	public List<Person> getScrollData(int offset,int maxResult){
    		SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
    		List<Person> persons=new ArrayList<Person>();
    		Cursor cursor=db.rawQuery("select * from person order by personid asc limit?,?", 
    				new String[]{String.valueOf(offset),String.valueOf(maxResult)});
    		while(cursor.moveToNext()){
    			int personid=cursor.getInt(cursor.getColumnIndex("personid"));
    			String name=cursor.getString(cursor.getColumnIndex("name"));
    			String phone=cursor.getString(cursor.getColumnIndex("phone"));
    			int amount=cursor.getInt(cursor.getColumnIndex("amount"));
    			persons.add(new Person(personid, name, phone,amount));
    		}
    		cursor.close();
    		return persons;
    	}
    

      3、单击一条数据事件

    protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		service=new PersonService(this);
    		listView=(ListView)this.findViewById(R.id.listview);
    		show();
    		listView.setOnItemClickListener(new ItemClickListener());//单击事件
    		
    	}
    	
    	private final class ItemClickListener implements OnItemClickListener{
    
    		@Override
    		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    				long arg3) {
    			ListView listView=(ListView)arg0;
    			//SimpleAdapter适配器
    			HashMap<String, Object> item=(HashMap<String, Object>)listView.getItemAtPosition(arg2);
    			String name=item.get("name").toString();
    			Toast.makeText(getApplicationContext(), name, 2).show();
    					
    		}
    		
    	}  
    

      

  • 相关阅读:
    ElasticSearch入门 第一篇:Windows下安装ElasticSearch
    Elasticsearch+Logstash+Kibana教程
    MySQL组合索引最左匹配原则
    mysql 有哪些索引
    MySQL配置优化
    MySQL分区和分表
    MySQL优化
    MySQL锁详解
    MySQL各存储引擎
    MySQL索引类型
  • 原文地址:https://www.cnblogs.com/wdc224/p/3929583.html
Copyright © 2011-2022 走看看