适配器

android

2013-07-09 16:22

ArrayAdapter    


listView1=(ListView)this.findViewById(R.id.listView1);

ArrayList<String> ls=new ArrayList<String>();    动态数组

ls.add("a");

ls.add("b");


ArrayAdapter ad = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,ls);

listView1.setAdapter(ad);




SimpleAdapter

SimpleAdapter smad;

ArrayList<HashMap<String,String>> ll=new ArrayList<HashMap<String,String>>();


while(i<=10){

HashMap<String, String> h= new HashMap<String, String>();

h.put("title","A"+i);

h.put("content","B"+i);

ll.add(h);

}

String[] from= new String[]{"title","content"};

int[] to=new int[]{R.id.textView11,R.id.textView22};

smad=new SimpleAdapter(this, ll, R.layout.l, from, to);

listView1.setAdapter(smad);


SimpleCursorAdapter sca = new SimpleCursorAdapter(this,R.layout.layout,cursor,from,to); 

listView1.setAdapter(sca);


BaseAdapter

        lv=(ListView)findViewById(R.id.listView1);
        getdate();
        my=new my(this,datev);
        lv.setAdapter(my);


public void getdate(){
      datev = new ArrayList<HashMap<String,Object>>();
  for(int i=0;i<d1.length;i++){
          
           HashMap<String, Object> hm=new HashMap<String, Object>();
           hm.put("tv", d1[i]);
           hm.put("iv", d2[i]);
           hm.put("rb", d3[i]);
           hm.put("cb", d4[i]);
           datev.add(hm);
       }
 }


public class my extends BaseAdapter {

    public Activity ac;
    public ArrayList<HashMap<String, Object>> data;
    public my(Activity ac,ArrayList<HashMap<String, Object>> data){
        
        this.ac=ac;
        this.data=data;
        
    }
    
    @Override
    public int getCount() {
    return data.size();
    }

    @Override
    public Object getItem(int position) {

        return data.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        ViewHolder holder=null;
         if (null == convertView) {
              holder = new ViewHolder();
              convertView= LayoutInflater.from(ac).inflate(R.layout.list, null);;              
               holder.iv= (ImageView)convertView.findViewById(R.id.iv);
               holder.tv= (TextView)convertView.findViewById(R.id.tv);
               holder.rb= (RatingBar)convertView.findViewById(R.id.rb);
               holder.cb= (CheckBox)convertView.findViewById(R.id.cb);
               convertView.setTag(holder);
             
         } else {
             holder = (ViewHolder) convertView.getTag();
            
         }
    
         HashMap<String, Object> hh= data.get(position);
         holder.id=position;   
         holder.tv.setText(hh.get("tv").toString());   
         holder.cb.setChecked((Boolean) hh.get("cb"));   
         holder.iv.setImageDrawable(this.ac.getResources().getDrawable((Integer) hh.get("iv")));
         holder.rb.setProgress((Integer) hh.get("rb"));
        Log.d("BOK", "标题="+hh.get("tv").toString()+"状态:"+hh.get("cb")+"图片:"+(Integer) hh.get("iv")+"分数"+(Integer) hh.get("rb"));
        
        return convertView;
    }

    
     public final class ViewHolder {  
            public int id;
            public ImageView iv;  
            public TextView tv;  
            public CheckBox cb;  
            public RatingBar rb;
        }  
    
    
}