2010-09-07 111 views
0

我正在構建聯繫人列表,用戶可以在其中選擇多個聯繫人。 目前的android佈局只提供了一個單一的textview和一個複選框的多選擇,我想要做的是有名稱和編號和一個複選框(兩個textview和一個複選框)。當我使用自定義佈局實現此功能並且用戶單擊列表時,複選框不會被打勾。我試圖將複選框綁定到列表視圖,但它沒有奏效。任何幫助將非常感激。Android Listview,具有自定義多選的光標適配器

回答

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); 
      }        
     }    
    });  
相關問題