Android Listview,带自定义多选的游标适配器

时间:2010-09-07 04:00:39

标签: android listview adapter checkboxlist

我正在构建联系人列表,用户可以在其中选择多个联系人。 目前Android布局仅提供带有单个textview和复选框的多选,我想要做的是具有名称和编号以及复选框(两个textviews和一个复选框)。当我使用自定义布局实现此功能时,当用户单击列表时,复选框不会被勾选。我试图将复选框绑定到列表视图但它没有用。任何帮助将非常感激。

2 个答案:

答案 0 :(得分:0)

private class EfficientAdapter extends BaseAdapter implements ListView.OnScrollListener       
{


    private LayoutInflater mInflater;

     public EfficientAdapter(Context context) {
         mInflater = LayoutInflater.from(context);  

     }

     public int getCount() {            
         return c.getCount();
     }

     public Object getItem(int position) {           

         return position;
     }

     public long getItemId(int position) {
         return position;
     }  

     public View getView(int position, View convertView, ViewGroup parent) {

         ViewHolder holder;

         if (convertView == null) {

             convertView = mInflater.inflate(R.layout.customcontactlist, null);              
             holder = new ViewHolder();               
             holder.txtName = (CheckedTextView) convertView.findViewById(R.id.TextView01);
             holder.txtNumber = (TextView) convertView.findViewById(R.id.TextView00);                
             convertView.setTag(holder);    

         } else {
             holder = (ViewHolder) convertView.getTag();
         }


         if (hashtable.contains(position)) {
             holder.txtName.setChecked(true);
         }else{
             holder.txtName.setChecked(false);
         }
         return convertView;
     }

      class ViewHolder {
         CheckedTextView txtName;
         TextView txtNumber;         
       }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
        }

        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }                   
     }

和活动

l1.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {          

            EfficientAdapter.ViewHolder holder;
            holder = (EfficientAdapter.ViewHolder) arg1.getTag();


            if(holder.txtName.isChecked()){
                holder.txtName.setChecked(false);
            }else{
                holder.txtName.setChecked(true);
            }                               
        }               
    });     

答案 1 :(得分:0)

我在类似的stackoverflow问题(Multiple choice list with custom view?)中找到了解决此类问题的通用方法:

http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/