0

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

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;
                                        }

                                    }

                                }
                            });
4

5 回答 5

1

这就是我实现相同的方式,但我使用图像而不是内置复选框来选中/取消选中列表项。

我的列表视图的单行包含 TextView 和 Imageview。

下面是 ArrayAdapter 和 listview 的 onItemClick() 事件的代码:

static class Category {

        String cat_name = "";
        int cat_id = 0;
        Boolean checked = false;

        Category(int cat_id, String name) {
            this.cat_name = name;
            this.cat_id = cat_id;
        }

        public int getId() {
            return cat_id;
        }

        public String getCatName() {
            return cat_name;
        }

        public Boolean getChecked() {
            return checked;
        }

        public void setChecked(Boolean checked) {
            this.checked = checked;
        }

        public boolean isChecked() {
            return checked;
        }

        public void toggleChecked() {
            checked = !checked;
        }
    }

    static class CategoryViewHolder {

        ImageView imageViewCheck;
        TextView textViewCategoryName;

        public CategoryViewHolder(ImageView iv_check, TextView tv_category_name) {
            imageViewCheck = iv_check;
            textViewCategoryName = tv_category_name;
        }

        public ImageView getImageViewCheck() {
            return imageViewCheck;
        }

        public TextView getTextViewCategoryName() {
            return textViewCategoryName;
        }
    }

    static class CategoryArrayAdapter extends ArrayAdapter<Category> {

        LayoutInflater inflater;

        public CategoryArrayAdapter(Context context, List<Category> categoryList) {

            super(context, R.layout.single_row_delete_data,
                    R.id.textViewSingleRowDeleteData, categoryList);
            inflater = LayoutInflater.from(context);
        }

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

            Category category = (Category) this.getItem(position);

            final ImageView imageViewCheck;
            final TextView textViewCN;

            if (convertView == null) {

                convertView = inflater.inflate(R.layout.single_row_delete_data,
                        null);

                imageViewCheck = (ImageView) convertView
                        .findViewById(R.id.imageViewSingleRowDeleteData);
                textViewCN = (TextView) convertView
                        .findViewById(R.id.textViewSingleRowDeleteData);

                convertView.setTag(new CategoryViewHolder(imageViewCheck,
                        textViewCN));
            }

            else {

                CategoryViewHolder viewHolder = (CategoryViewHolder) convertView
                        .getTag();
                imageViewCheck = viewHolder.getImageViewCheck();
                textViewCN = viewHolder.getTextViewCategoryName();
            }

            imageViewCheck.setFocusable(false);
            imageViewCheck.setFocusableInTouchMode(false);
            imageViewCheck.setClickable(true);
            imageViewCheck.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    ImageView iv = (ImageView) v;
                    Category category = (Category) iv.getTag();

                    if (category.getChecked() == false) {
                        imageViewCheck.setImageResource(R.drawable.set_check);
                        listOfItemsToDelete.add(category.getId());
                        category.setChecked(true);
                    } else {
                        imageViewCheck
                                .setImageResource(R.drawable.set_basecircle);
                        listOfItemsToDelete.remove((Integer) category.getId());
                        category.setChecked(false);
                    }
                }
            });
            imageViewCheck.setTag(category);

            if (category.getChecked() == true)
                imageViewCheck.setImageResource(R.drawable.set_check);
            else
                imageViewCheck.setImageResource(R.drawable.set_basecircle);

            textViewCN.setText(category.getCatName());

            return convertView;
        }
    }

ListView 的onItemClick()事件:

lv_delete_data.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                ImageView imageView = (ImageView) arg1
                        .findViewById(R.id.imageViewSingleRowDeleteData);
                Category category = (Category) imageView.getTag();

                if (category.getChecked() == false) {
                    imageView.setImageResource(R.drawable.set_check);
                    listOfItemsToDelete.add(category.getId());
                    category.setChecked(true);
                } else {
                    imageView.setImageResource(R.drawable.set_basecircle);
                    listOfItemsToDelete.remove((Integer) category.getId());
                    category.setChecked(false);
                }
            }
        });

这就是我实现的方式,与您需要的相同。如果您有任何疑问可以问我。

于 2013-05-03T10:12:06.553 回答
0

看看这里。本教程介绍如何向 listViews 添加复选框并处理更新和检查其状态

于 2013-05-03T10:14:39.443 回答
0

您将不得不玩setTag()getTag()。我想推荐你阅读这些帖子:

Android:更改列表视图中特定项目的图像在通过列表视图检查动态生成的复选框时遇到问题

于 2013-05-03T10:07:11.157 回答
0

你可以试试这个...

List<String> ids = new ArrayList<String>();
holder.check_favourite.setId(position);


        if (ids.contains(holder.check_favourite.getId() + "")) {
            holder.check_favourite.setChecked(true);
        } else {
            holder.check_favourite.setChecked(false);
        }
        int id= 0;

        holder.check_favourite
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                            }

                        }

                    }
                });

使用 if 条件设置复选框 true 或 false 以便在您滚动列表视图时不会取消选中您的复选框...这是我的代码为您的复选框应用相同的代码..希望您得到它...

于 2013-05-03T09:49:57.627 回答
0

如果您的意图是获取或存储检查项目,您可以使用数组适配器。查看此代码。它可能会对您有所帮助。

/定义复选框点击事件监听器/

OnClickListener clickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        CheckBox chk = (CheckBox) v;

        int itemCount = getListView().getCount();

        for(int i=0 ; i < itemCount ; i++){

            getListView().setItemChecked(i, chk.isChecked());
        }
    }

};

/**Defining click event listener for the listitem checkbox**/

OnItemClickListener itemClickListener = new OnItemClickListener() {

@Override

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

    CheckBox chk = (CheckBox) findViewById(R.id.chkAll);

    int checkedItemCount = getCheckedItemCount();

    if(getListView().getCount()==checkedItemCount)

        chk.setChecked(true);

    else

        chk.setChecked(false);
    }
};

/**Getting reference to checkbox available in the main.xml layout**/

CheckBox chkAll = ( CheckBox ) findViewById(R.id.chkAll);

/**Setting a click listener for the checkbox**/

chkAll.setOnClickListener(clickListener);

/**Setting a click listener for the listitem checkbox**/

 ` getListView().setOnItemClickListener(itemClickListener); `

}

/返回检查项的数量/

private int getCheckedItemCount() {

int cnt = 0;

SparseBooleanArray positions = getListView().getCheckedItemPositions();

int itemCount = getListView().getCount();

for(int i=0;i<itemCount;i++){

    if(positions.get(i))

        cnt++;
}

return cnt;

}

要更新选中的项目,请使用

SparseBooleanArray checked = getListView().getCheckedItemPositions();

于 2013-07-05T06:35:00.977 回答