充分利用Android的列表视图所有选中的项目 [英] Getting all checked items from listview Android

查看:112
本文介绍了充分利用Android的列表视图所有选中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器人编程noobie,我使用Android的工作室,我有包含两个textviews和复选框列表视图项。
我试图创建一个复选框,以便listiview当一个特定的按钮点击我会得到一个列表中选中的项目的名称。
试图寻找教程和其他有用的东西,但只是无法得到它的权利。
谢谢大家 :)
这是我为ListView自定义适配器:

 公共类postadapter扩展ArrayAdapter<邮电GT; {
ArrayList的<邮电GT;名单=新的ArrayList<信息>();
上下文语境;
INT layoutResourceId;
发布数据[] = NULL;公共postadapter(上下文的背景下,诠释layoutResourceId,ArrayList的<信息>名单){
    超(背景下,layoutResourceId,清单);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.list =清单;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看排= convertView;
    ViewHolder支架=无效;    如果(行== NULL){
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
        行= inflater.inflate(layoutResourceId,父母,假);        持有人=新ViewHolder();
        holder.txtTitle =(TextView中)row.findViewById(R.id.textView1);
        holder.txtdescription =(TextView中)row.findViewById(R.id.textView2);
        holder.chk =(复选框)row.findViewById(R.id.chkbox);        row.setTag(保持器);
        holder.chk.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                复选框CB =(复选框)V;
                后POST1 =(后)cb.getTag();                post1.setSelected(cb.isChecked());
            }
        });
    }
    其他
    {
        支架=(ViewHolder)row.getTag();
    }    发布POST1 = list.get(位置);
    holder.txtTitle.setText(post1.getPostname());
    holder.txtdescription.setText(מספרשומרים:+ post1.getnumberguards()+משעה:+ post1.getstime()+עדשעה:+ post1.getetime());
holder.chk.setChecked(post1.isSelected());
    返回行;
}静态类ViewHolder
{    TextView的txtTitle;
    TextView的txtdescription;
    复选框CHK;}
 }


解决方案

您可以保持所有选择复选框 - 一个布尔数组


  1. 声明布尔数组在适配器 - 布尔[] checkBoxState

  2. 初始化它在你的适配器的构造 - checkBoxState =新的布尔[则为list.size()]

  3. 然后在你的getView方法使用这个数组 -

      holder.checkBox.setOnClickListener(新View.OnClickListener(){
     公共无效的onClick(视图v)
     {
                    如果(((复选框)V).isChecked())
                    {
                         checkBoxState [位置] =真;
                        }
                        其他
                        {
                            checkBoxState [位置] = FALSE;                    }
                       }
                       });


  4. 从这个数组中的位置(这里适配器是您的自定义接口的对象) -

    有关(INT K = 0; K<
    adapter.checkBoxState.length; k ++)
            {

    如果(adapter.checkBoxState [K] ==真)
                            {
                              }`


I am a noobie at android programming , I am using Android studio and I have a listview item containing two textviews and a checkbox. I am trying to create a listiview with checkboxes so when a click on a specific button I will get a list the names of the checked items . Tried looking for tutorials and other helpful things but just can't get it right. Thank You everyone :) This is my custom adapter for the listview:

public class postadapter extends ArrayAdapter<post> {
ArrayList<post> list = new ArrayList<post>();
Context context;
int layoutResourceId;
post data[] = null;

public postadapter(Context context, int layoutResourceId, ArrayList<post> list) {
    super(context, layoutResourceId, list);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.list = list;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;

    if(row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ViewHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.textView1);
        holder.txtdescription = (TextView) row.findViewById(R.id.textView2);
        holder.chk = (CheckBox) row.findViewById(R.id.chkbox);

        row.setTag(holder);
        holder.chk.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                post post1 = (post) cb.getTag();

                post1.setSelected(cb.isChecked());
            }
        });
    }
    else
    {
        holder = (ViewHolder)row.getTag();
    }

    post post1 = list.get(position);
    holder.txtTitle.setText(post1.getPostname());
    holder.txtdescription.setText("מספר שומרים:"+post1.getnumberguards()+" משעה: "+post1.getstime()+" עד שעה: "+post1.getetime());
holder.chk.setChecked(post1.isSelected());
    return row;
}

static class ViewHolder
{

    TextView txtTitle;
    TextView txtdescription;
    CheckBox chk;

}
 }

解决方案

You can maintain a boolean array of all the selected checkbox-

  1. Declare Boolean array in your adapter - boolean[] checkBoxState
  2. Initialize it in your adapter's constructor - checkBoxState= new boolean[list.size()]
  3. Then use this array in your getView method -

     holder.checkBox.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v)
     {
                    if(((CheckBox)v).isChecked())
                    {                   
                         checkBoxState[position]=true;
                        }       
                        else
                        {
                            checkBoxState[position]=false;
    
                        }
                       }
                       });
    

  4. Retrieve the position from this array (Here adapter is the object of your custom adapter) -

    for(int k=0;k< adapter.checkBoxState.length ;k++) {

    if(adapter.checkBoxState[k]==true) { }`

这篇关于充分利用Android的列表视图所有选中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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