zoukankan      html  css  js  c++  java
  • android学习笔记之ListView 和Adapter适配器BaseAdapter,simpleAdapter、adrrayAdapter

    1、在学习Listview时候用到了Adapter适配器,定义MyAdapter时候需要继承ListAdapter接口,接口里很多方法没有实现,为了方便google工程师实现了个BaseAdapter类,我们在使用的时候可以继承这个抽象类,因此我们只需要完成几个抽象方法就可以了。

    public class Db_adapter extends BaseAdapter {
    private Context context;
    private List<Person> personlist;
    	
    public Db_adapter(Context context,List<Person> personlist)
    	{
    	this.context=context;
    	this.personlist=personlist;
    	}
    public void setPersonlist(List<Person> personlist)
    {
    	this.personlist=personlist;
    }
    	@Override
    	public int getCount() {
    		// TODO Auto-generated method stub
    		return personlist.size();
    	}
    
    	@Override
    	public Object getItem(int position) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public long getItemId(int position) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		// TODO Auto-generated method stub
    		TextView text=new TextView(context);
    		text.setText(personlist.get(position).getName());
    //		TextView text2=new TextView(context);
    //		text2.setText(personlist.get(position).getMoney()+"");
    		return text;
    		
    //		return null;
    	}
    }
    

      2、其中最关键的是完成getView()方法,getView方法调用的次数不是getcount方法的返回值,当listView里的条目太多时候,这里android虚拟机会自动计算ListView控件的高端h1,然后计算出TextView的高度h2,因此一开始就会调用h1/h2次getview()方法,当我们上下拖动时候,出现我们先看不到的条目时候就调用getview(),即使先前出现了的条目在下拖过程中不见,然后回托时候又出现时候仍会调用getview()方法。

      3、利用simpleAdapter适配器它是继承BaseAdapter的。值得注意的是,如下面红色代码部分,在item里textview赋值是根据前面的String数组的顺序来的,如下代码,则会把tel的值赋给tv_money控件,当map里没有tel字段时候则只显示name控件。 List<Map<String,String>>data=new ArrayList<Map<String,String>>();

    for(Person person:personList)
    {
      Map<String,String> map=new HashMap<String, String>();
      map.put("name", person.getName());
      map.put("tel", person.getTel()+"");
      map.put("money", person.getMoney()+"");
      data.add(map);
    }
    personListView.setAdapter(new SimpleAdapter(this, data, R.layout.item2,
    new String[]{"name","tel"}, new int[]{R.id.tv_name,R.id.tv_money}));

      4、adrrayAdapter最方便的,但只能显示单个的列信息,具体在new adrrayAdapter();查看api说明!

  • 相关阅读:
    hello word
    HDU 1006 Tick and Tick
    HDU 1005 Number Sequence
    HDU 1004 Let the Balloon Rise
    HDU 1003 Max Sum
    HDU 1002 A + B Problem II
    HDU 1001 Sum Problem
    HDU 1000 A + B Problem
    POJ 2253 Frogger
    POJ 2387 Til the Cows Come Home
  • 原文地址:https://www.cnblogs.com/bokeofzp/p/4658703.html
Copyright © 2011-2022 走看看