注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

CheckBox列表的使用实例  

2010-05-19 10:30:33|  分类: 数据适配器 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sec.android.touchwiz.samples.R;

public class ListLayoutSample3Xml extends Activity {
    
    ListView mListView;
    private final String LOG_TAG_LISTLAYOUTSAMPLE3 = "ListLayoutSample3Xml";
        
        
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlayoutsample3xml_main);
        
        mListView = (ListView)findViewById(R.id.contactCheckList);
        // [09.10.23] ListView? choiceMode? CHOICE_MODE_MULTIPLE? ???? 
        // ??? item? check?? ??? ?
        // [E] Set ListView's choiceMode to CHOICE_MODE_MULTIPLE for multi check.
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        ArrayList<ContactItem> list = new ArrayList<ContactItem>();      
        
        for(int i=0;i<mName.length;i++){
            ContactItem contact = new ContactItem(mThumbs, mName);
            list.add(contact);
        }    
        
        // ContactAdapter ??
        ContactAdapter cAdapter = new ContactAdapter(this, R.layout.listlayoutsample3xml_row, list);
        // ListView? ContactAdapter? ??
        mListView.setAdapter(cAdapter);

    }
    
    /*
     * ContactItem? ??? Thumbnail data 
     */
    private Integer[] mThumbs = {
            R.drawable.listlayoutsample3xml_1, 
            R.drawable.listlayoutsample3xml_2, 
            R.drawable.listlayoutsample3xml_3,
            R.drawable.listlayoutsample3xml_4, 
            R.drawable.listlayoutsample3xml_5, 
            R.drawable.listlayoutsample3xml_seankingston,
            R.drawable.listlayoutsample3xml_karina, 
            R.drawable.listlayoutsample3xml_jayz, 
            R.drawable.listlayoutsample3xml_madonna};
    
    /*
     * ContactItem? ??? FullName data 
     */
    private String[] mName = 
    {
            "Anna Bay",
            "Agatha Christina",
            "Agnes Green",
            "Mom",
            "July",
            "Sean Kingston",
            "Karina",
            "Jay-Z",
            "Madonna"
    };
    
    
    /*
     * ListView? ???? Adapter. ArrayAdapter? ???? ??.
     * ContactItem type? data?? ArrayList? ???.
     */
    private class ContactAdapter extends ArrayAdapter<ContactItem> {

        private ArrayList<ContactItem> items;

        public ContactAdapter(Context context, int textViewResourceId, ArrayList<ContactItem> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listlayoutsample3xml_row, null);
                }
                ContactItem ti = items.get(position);
                if (ti != null) {
                        ImageView thumbview = (ImageView) v.findViewById(R.id.photo1);
                        TextView nameview = (TextView) v.findViewById(R.id.text1);
                        final CheckBox check = (CheckBox) v.findViewById(R.id.checkbox1);
                        
                        if (thumbview != null){
                            thumbview.setImageResource(ti.getContactThumb());                            
                        }
                        if (nameview != null){
                            nameview.setText(ti.getFullName());                            
                        }
                        
                        if(check != null) {
                            final int myPosition = position;
                            check.setOnClickListener(new OnClickListener() {
                                
                                public void onClick(View v) {
                                    check.post(new Runnable() {
                                        public void run() {
                                            boolean fchecked = check.isChecked();
                                            onBtnCheckChanged(myPosition, fchecked);        
                                        }
                                    });
                                    
                                }
                            });
                        }
                        
                }
                return v;
        }
    }
    
    private void onBtnCheckChanged(int position, boolean check) {
        Log.d(LOG_TAG_LISTLAYOUTSAMPLE3,"onBtnCheckChanged("+position+","+check+")");        
        mListView.setItemChecked(position, check);
    }
    
    /**
     * @author cooldawn.kim
     * 
     * Contact Item
     */
    class ContactItem{
        private Integer mContactThumb;
        private String mFullName;
        
        public ContactItem( Integer thumb, String fullName )
        {
            this.mContactThumb = thumb;
            this.mFullName = fullName;
        }
        
        public Integer getContactThumb(){
            return mContactThumb;
        }
        
        public String getFullName(){
            return mFullName;
        }
        
    } // class ContactItem
    
    public static class ItemView extends LinearLayout implements Checkable {

        public ItemView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public boolean isChecked() {
            CheckBox check = findCheckBox();
            if(check != null) {
                return check.isChecked();
            }                
            return false;
        }
        
        public void setChecked(boolean checked) {
            Log.d("ItemView", "setChecked("+checked+")");
            CheckBox check = findCheckBox();
            if(check != null) {
                check.setChecked(checked);
            }                
        }

        public void toggle() {
            CheckBox check = findCheckBox();
            if(check != null) {
                check.toggle();
            }
                
        }
        
        private CheckBox findCheckBox() {
            View check = findViewById(R.id.checkbox1);
            if(check != null && check instanceof CheckBox) {
                return (CheckBox)check;
            }
            return null;
        }
        
    }

}
  评论这张
 
阅读(593)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017