适配器及适配器控件

ArrayAdapter适配器:


在编写时,先获得ListView对象,然后创建ArrayAdapter适配器,通过ArrayAdapter适配器将准备好的数组中的数据与ListView连接起来;接着将ArrayAdapter添加到ListView中,最后给ListView添加OnItemSelectListener选中监听,添加每一项被选中或的动作.

ArrayAdapter内部在得到每一个列表项View时,是通过先实例化layout布局,再从布局中获取TextView控件,所以说布局中有无其他控件是没关系的,但是ArrayAdapter只负责对每个列表项中的TextView进行数据填充。

总的来说,ArrayAdapter是最简单的ListView的适配器,内部只有一个TextView,而且参数我们一般使用android内部的id变量android.R.layout.simple_list_item_1。


ListViewAdapter:

private static final String TAG = ListViewDemoAdapter.class.getSimpleName();  
  
    /** 
     * 上下文 
     */  
    private Context mContext;  
  
    /** 
     * LayoutInflater 
     */  
    private LayoutInflater mInflater;  
  
    private List<String> datas;  
  
    public ListViewDemoAdapter(Context context,List<String> list){  
        this.mContext = context;  
        this.datas = list;  
        this.mInflater = LayoutInflater.from(context);  
    }  
  
    @Override  
    public int getCount() {  
        return datas.size();  
    }  
  
    @Override  
    public String getItem(int position) {  
        return datas.get(position);  
    }  
  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        ViewHolder holder = null;  
        if(convertView == null){  
            holder = new ViewHolder();  
            convertView = mInflater.inflate(R.layout.listitem,null);  
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);  
            convertView.setTag(holder);  
        }else{  
            holder = (ViewHolder) convertView.getTag();  
        }  
        holder.tv_name.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                Log.d(TAG,"setOnClickListener-->onClick...");  
                //回调传递点击的view  
                mItemOnClickListener.itemOnClickListener(v);  
            }  
        });  
        return convertView;  
    }  
  
    private ItemOnClickListener mItemOnClickListener;  
  
    public void setmItemOnClickListener(ItemOnClickListener listener){  
        Log.d(TAG,"setmItemOnClickListener...");  
        this.mItemOnClickListener = listener;  
    }  
  
    public interface ItemOnClickListener{  
        /** 
         * 传递点击的view 
         * @param view 
         */  
        public void itemOnClickListener(View view);  
    }  
  
    public class ViewHolder{  
        public TextView tv_name;  
    }  
private static final String TAG = ListViewDemoAdapter.class.getSimpleName();  
  
    /** 
     * 上下文 
     */  
    private Context mContext;  
  
    /** 
     * LayoutInflater 
     */  
    private LayoutInflater mInflater;  
  
    private List<String> datas;  
  
    public ListViewDemoAdapter(Context context,List<String> list){  
        this.mContext = context;  
        this.datas = list;  
        this.mInflater = LayoutInflater.from(context);  
    }  
  
    @Override  
    public int getCount() {  
        return datas.size();  
    }  
  
    @Override  
    public String getItem(int position) {  
        return datas.get(position);  
    }  
  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        ViewHolder holder = null;  
        if(convertView == null){  
            holder = new ViewHolder();  
            convertView = mInflater.inflate(R.layout.listitem,null);  
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);  
            convertView.setTag(holder);  
        }else{  
            holder = (ViewHolder) convertView.getTag();  
        }  
        holder.tv_name.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                Log.d(TAG,"setOnClickListener-->onClick...");  
                //回调传递点击的view  
                mItemOnClickListener.itemOnClickListener(v);  
            }  
        });  
        return convertView;  
    }  
  
    private ItemOnClickListener mItemOnClickListener;  
  
    public void setmItemOnClickListener(ItemOnClickListener listener){  
        Log.d(TAG,"setmItemOnClickListener...");  
        this.mItemOnClickListener = listener;  
    }  
  
    public interface ItemOnClickListener{  
        /** 
         * 传递点击的view 
         * @param view 
         */  
        public void itemOnClickListener(View view);  
    }  
  
    public class ViewHolder{  
        public TextView tv_name;  
    }  

MainActivity:

public class MainActivity extends AppCompatActivity {  
  
    private static final String TAG = MainActivity.class.getSimpleName();  
  
    /** 
     * listview 
     */  
    private ListView mLvDemo;  
  
    private List<String> list = new ArrayList<String>();  
  
    private  ListViewDemoAdapter mAdapter;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        //初始化view  
        initView();  
        //初始化数据  
        initData();  
    }  
  
    /** 
     * 初始化view 
     * @author crystal 
     * @date 2015-9-4 22:24 
     */  
    private void initView(){  
        //初始化listview  
        mLvDemo = (ListView) findViewById(R.id.lv_demo);  
        mAdapter = new ListViewDemoAdapter(MainActivity.this,list);  
        mLvDemo.setAdapter(mAdapter);  
  
        //获取点击的view子控件  
        mAdapter.setmItemOnClickListener(new ListViewDemoAdapter.ItemOnClickListener() {  
  
            /** 
             *  点击的view子控件 
             * @param view view子控件 
             */  
            @Override  
            public void itemOnClickListener(View view) {  
                Log.d(TAG, "itemOnClickListener view...");  
                TextView tvName = (TextView)view;  
                //提示点击的子控件的文本  
                Toast.makeText(MainActivity.this,tvName.getText().toString(),Toast.LENGTH_LONG).show();  
            }  
        });  
    }  
  
    /** 
     * 初始化数据 
     * @author crystal 
     * @date 2015-9-4 22:51 
     */  
    private void initData(){  
        list.add("crystal");  
        list.add("jack");  
        list.add("tom");  
        list.add("rose");  
        list.add("lora");  
        list.add("tony");  
        mAdapter.notifyDataSetChanged(); //更新数据  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.menu_main, menu);  
        return true;  
    }  
  
    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
        // Handle action bar item clicks here. The action bar will  
        // automatically handle clicks on the Home/Up button, so long  
        // as you specify a parent activity in AndroidManifest.xml.  
        int id = item.getItemId();  
  
        //noinspection SimplifiableIfStatement  
        if (id == R.id.action_settings) {  
            return true;  
        }  
  
        return super.onOptionsItemSelected(item);  
    }  
}  

activity_main.xml   :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
  
    <ListView  
        android:id="@+id/lv_demo"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent">  
    </ListView>  
</LinearLayout> 

item.xml   :

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:padding="15dp"  
    android:gravity="center_horizontal"  
    android:orientation="horizontal">  
  
    <TextView  
        android:id="@+id/tv_name"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="crystal"/>  
  
</LinearLayout>  


SimpleAdapter适配器:


  1. 根据需要定义ListView每行所实现的布局。
  2. 定义一个HashMap构成的列表,将数据以键值对的方式存放在里面。
  3. 构造SimpleAdapter对象。
  4. 将LsitView绑定到SimpleAdapter上。
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv=(ListView) findViewById(R.id.lv);

        SimpleAdapter simple=new SimpleAdapter(this, getData(),
                R.layout.list_view_item, new String[]{"key1","key2"}, new int[]{R.id.textView1,R.id.textView2});
        lv.setAdapter(simple);
    }

    private ArrayList<Map<String, Object>> getData(){
        ArrayList<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
        Map<String,Object> hashmap=new HashMap<String, Object>();
        for(int i=0;i<5;i++){
            hashmap.put("key1", "data1");
            hashmap.put("key2", "data2");
            list.add(hashmap);
        }
        return list;
    }


BaseAdapter适配器



当SimpleAdapter添加一个按钮到ListView的条目中,会发现可以添加,但是却无法获得焦点,点击操作被ListView的Item所覆盖。 

这时候最方便的方法就是使用灵活的适配器BaseAdapter了。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv=(ListView) findViewById(R.id.lv);

        lv.setAdapter(new BaseAdapter() {
      //每个item要显示在lv上的时候,会被调用的方法,安卓系统调用
      //     position    当前的item处于第几个位置
      //     convertView  用于做优化
      //     parent    当前的父控件
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view=View.inflate(MainActivity.this, R.layout.list_view_item, null);
                TextView tv1=(TextView) view.findViewById(R.id.textView1);
                tv1.setText("我是第:"+position+"个");
                return view;
            }

            //这个方法是最开始被调用的,用来计算到底有多少个item需要显示在lv上
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 20;
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值