0

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

4

2 回答 2

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);
            }                               
        }               
    });     
于 2010-10-15T12:24:42.473 回答
0

我在类似的stackoverflow问题(带自定义视图的多项选择列表? )中找到了解决此类问题的通用方法:

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

于 2011-02-18T15:05:02.330 回答