Android Listview,具有自定义多选功能的光标适配器

发布于 2024-09-17 22:44:34 字数 174 浏览 8 评论 0原文

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

I'm building a list of contacts, where the user can select more than one one contact.
Currently the android layouts only provide a multiple select with a single textview and a checkbox, what I want to do is have the name and number and a checkbox (two textviews and a checkbox). When I implement this with a custom layout, and when the user clicks on the list, the check boxes don't get ticked. I tried to bind the checkbox to the listview but it didnt work. Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2024-09-24 22:44:34

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

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

I found a generic method to solve this kind of problem in a similar stackoverflow question ( Multiple choice list with custom view? ):

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

一个人练习一个人 2024-09-24 22:44:34
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);
            }                               
        }               
    });     
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) {

        }                   
     }

and in Activity

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);
            }                               
        }               
    });     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文