Android 用户界面数据的绑定--Adapter


在开发中,我们经常使用到ListView这个控件。Android的API也提供了许多创建ListView适配器的快捷方式。例如ArrayAdapter、SimpleAdapter和SimpleCursorAdapter等。但你是否发现,如果采用这些系统自带的适配器,对于事件的响应只能局限在一个行单位。假设一行里面有一个按钮和一个图片控件,它们之间的响应操作是不一样的。若采用系统自带的适配器,就不能精确到每个控件的响应事件。这时,我们一般采取自定义适配器来实现这个比较精确地请求。
       ListView的创建,一般要具备两大元素:
       1)数据集,即要映射的字符串、图片信息之类。
       2)适配器,实现把要映射的字符串、图片信息映射成视图(如Textview、Image等组件),再添加到ListView中。

具体示例:

1.创建适配器 

    public class UserAdapter extends BaseAdapter{

        @Override
        
public int getCount() {
            
// TODO Auto-generated method stub
            return userList.size();
        }

        @Override
        
public Object getItem(int position) {
            
// TODO Auto-generated method stub
            return userList.get(position);
        }

        @Override
        
public long getItemId(int position) {
            
// TODO Auto-generated method stub
            return position;
        }

        @Override
        
public View getView(int position, View convertView, ViewGroup parent) {
            
// TODO Auto-generated method stub
            convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_user, null);
            ImageView iv 
= (ImageView)convertView.findViewById(R.id.iconImg);
            TextView tv 
= (TextView)convertView.findViewById(R.id.showName);
            UserInfo user 
= userList.get(position);
            
try{
                iv.setImageDrawable(user.getUserIcon());
                tv.setText(user.getUserName());
            }
catch (Exception e){
                e.printStackTrace();
            }
            
return convertView;
        }
        
    }

 2.ListView

View diaView = View.inflate(LoginActivity.this, R.layout.dialog2, null);
                dialog 
= new Dialog(LoginActivity.this,R.style.dialog2);
                dialog.setContentView(diaView);
                dialog.show();
                
                UserAdapter adapater 
= new UserAdapter();
                ListView listview
=(ListView)diaView.findViewById(R.id.list);
                listview.setVerticalScrollBarEnabled(
false);// ListView去掉下拉条
                listview.setAdapter(adapater);
                
                listview.setOnItemClickListener( 
new OnItemClickListener(){
                    @Override
                    
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        
// TODO Auto-generated method stub
                        TextView tv=(TextView)arg1.findViewById(R.id.showName);
                        iconSelect.setText(tv.getText());
                        ImageView iv
=(ImageView)arg1.findViewById(R.id.iconImg);
                        icon.setImageDrawable(iv.getDrawable());
                        dialog.dismiss();
                        
                    }
                    
                });
            }

 布局文件

dialog2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width
="wrap_content"
  android:layout_height
="wrap_content"
  android:orientation
="vertical"
  android:padding
="4dip">
  
<ListView
        
android:id="@+id/list"
        android:layout_width
="240px"
        android:layout_height
="220px"
        android:divider
="#f1f2f2"
        android:dividerHeight
="1px"
        android:layout_margin
="5px"
        android:background
="#ffffff"
        android:cacheColorHint
="#00000000">
      
</ListView>
</LinearLayout>

 

 item_user

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    
>
      
<ImageView android:id="@+id/iconImg"
                android:layout_width
="48dip"
                android:layout_height
="48dip" />

        
<TextView android:id="@+id/showName"
                android:layout_gravity
="center_vertical"
                android:layout_width
="0dip"
                android:layout_weight
="1"
                android:layout_height
="wrap_content" />
</LinearLayout>

 

 

 

posted on 2011-09-19 17:22  Alwaysyouare  阅读(2212)  评论(0编辑  收藏  举报

导航