带复选框Android的ListView [英] ListView with Checkboxes Android

查看:209
本文介绍了带复选框Android的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的活动ContactMailAdapter.class是我的列表视图适配器,其中包含复选框.如果我想获得已检查的行的位置.如何才能做到这一点.我的列表中有几个复选框,可以选中多个复选框.我的逻辑是我应该存储已检查的每个位置.然后从我的数组列表中调用数据,但是现在我无法从适配器获取选中项的位置.

The Activity below, ContactMailAdapter.class is my list view adapter containing checkbox. If i want to get the position of a row that has been checked. how can this be done. I have several checkboxes in my list and I can checked multiple checkbox. My logic is that I should store each position that has been checked. then call the data from my arraylist but for now I am not able to get the position of a checked item from my adapter.

public class ContactMailAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<Destinataire> data;
    private static LayoutInflater inflater = null;

    ViewHolder holder;
    static String src;

    PopMessage main;

    public ContactMailAdapter(Activity a, ArrayList<Destinataire> mArticles) {

        activity = a;
        data = mArticles;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }



    public ContactMailAdapter (PopMessage m){
                this.main=m;
    }


    @Override
    public int getCount() {
        return data.toArray().length;

    }

    @Override
    public Object getItem(int position) {

        return position;
    }

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

    public static class ViewHolder {

        public TextView one;
        public TextView two;
        public CheckBox check;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;

        if (convertView == null) {

            vi = inflater.inflate(R.layout.popise_item, null);
            holder = new ViewHolder();
            holder.one = (TextView) vi.findViewById(R.id.title_top);
            holder.two = (TextView) vi.findViewById(R.id.title_bottom);
            holder.check = (CheckBox)vi.findViewById(R.id.search_imag);
            vi.setTag(holder);

        } else
            holder = (ViewHolder) vi.getTag();


            holder.one.setText(data.get(position).getName());
            holder.two.setText(data.get(position).getEmail());




            vi.findViewById(R.id.search_imag).setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if (holder.check.isChecked())
                        System.out.println("false"+v.getId());
                    else 
                        System.out.println("false");

                }
            });


            holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()                  
                     {
                      @Override
                      public void onCheckedChanged(CompoundButton buttonView,
                              boolean isChecked) {



                      }
                  });



        /*  vi.findViewById(R.id.search_image).setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    v.findViewById(R.id.search_image).setBackgroundResource(R.drawable.boutton_off);
                    if (holder.check.isChecked())
                        System.out.println("true");
                    else 
                        System.out.println("false");

                }
            }); */



        return vi;
    }


}

新更新

ids = new ArrayList<String>();
            if (ids.contains(holder.check.getId() + "")) {
                        holder.check.setChecked(true);
                    } else {
                        holder.check.setChecked(false);
                    }

                    holder.check
                            .setOnCheckedChangeListener(new OnCheckedChangeListener() {



                                public void onCheckedChanged(CompoundButton buttonView,
                                        boolean isChecked) {
                                    // TODO Auto-generated method stub
                                    int id = buttonView.getId(); 
                                    if (isChecked) {
                                        if (ids.contains(id + "")) {
                                        } else {
                                            ids.add(id + "");
                                            id++;
                                            System.out.println( "receipe" + id);

                                        }
                                    } else {
                                        if (ids.contains(id + "")) {
                                            ids.remove(id + "");
                                            System.out.println( "receipe" + id);

                                          //  ids--;
                                        } else {
//                                        id = 0;
                                        }

                                    }

                                }
                            });

推荐答案

看看这个示例,它将为您提供一个完美的解决方案,我已使用它来获得项目中的选定位置.

Have a look at this sample this will provide you the perfect solution i have used it to get the selected position in my project. it worked like a charm with the concept of

checkbox.settag and gettag() methods. hope this will help you . 

http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html

这篇关于带复选框Android的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆