如何遍历android中listview的每一行中的每个复选框?

时间:2011-11-18 05:52:14

标签: android listview checkbox

我在listview的每一行都有一个带有三个textview和一个复选框的应用程序。我想要点击一个按钮,我就可以获得每个复选框的状态和对应于(isChecked)复选框的行删除。我的复选框在xml文件中被硬编码。我已经搜索了很多但是找不到任何具体的东西。谢谢。这是我的代码......

  public class recentcalllistultimate extends ListActivity implements OnClickListener {
            CheckBox cb;
            Button edit,done;

            ImageButton contacts;
            ListView lv;
            ListView lvultimate;
            listviewadapterultimate lvar;


            int[] uniqueid;
            String[] names;
            String[] types;
            ;
        RelativeLayout rl;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);

        LayoutParams params=newRelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
                LinearLayout mainLayout = new LinearLayout(this);
                mainLayout.setOrientation(LinearLayout.VERTICAL);
                LayoutInflater layoutInflater = getLayoutInflater();
                mainLayout.addView(layoutInflater.inflate(R.layout.listviewonly, null));
                mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
                this.addContentView(mainLayout, params);

                cb = (CheckBox) findViewById(R.id.checkboxdelete);

                getContacts();

                lv = (ListView) findViewById(android.R.id.list);
                lvar = new listviewadapterultimate(this, names, types,uniqueid);
                lv.setAdapter(lvar);

                contacts = (ImageButton) findViewById(R.id.button_keypad);
                contacts.setOnClickListener(this);

                edit = (Button) findViewById(R.id.editbutton);
                done=(Button)findViewById(R.id.donebutton);

                done.setOnClickListener(new View.OnClickListener() {



    ------>>>                          public void onClick(View v) {
                        // TODO Auto-generated method stub
          cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView, booleanisChecked) {
                                // TODO Auto-generated method stub

                               //WHAT TO DO HERE....        

                                }

                            }
                        });
                    }
     ------>>>          });


    edit.setOnClickListener(new View.OnClickListener() {

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

                        AddDialog ada=new AddDialog(recentcalllistultimate.this);
                        ada.show();
                    }
                });

        }// on create

            public void getContacts() {

                ContentResolver cr = getContentResolver();

                Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI, null,
                        null, null, null);

                if (cur.getCount() > 0) {
                    int i = 0;
                    int foo = 0;
                    names = new String[cur.getCount()];
                    types = new String[cur.getCount()];
                    duration = new long[cur.getCount()];
                    uniqueid = new int[cur.getCount()];

                    int n = cur.getColumnIndex(CallLog.Calls._ID);
                    int k = cur.getColumnIndex(CallLog.Calls.CACHED_NAME);
                    int y = cur.getColumnIndex(CallLog.Calls.NUMBER);
                    int z = cur.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE);


                    while (cur.moveToNext()) {

                        uniqueid[foo] = cur.getInt(n);

                        String str = cur.getString(k);

                        if (str == null) {
                            names[foo] = cur.getString(y);
                        }// if
                        else {
                            names[foo] = str;
                        }

                        int temp = cur.getInt(z);
                        switch (temp) {
                        case 0:
                            types[foo] = "unknown";
                            break;
                        case 1:
                            types[foo] = "home";
                            break;
                        case 2:
                            types[foo] = "mobile";
                            break;
                        case 3:
                            types[foo] = "work";
                            break;
                        }// switch

                        long doo = cur.getInt(d);
                        duration[foo] = doo;

                        foo++;

                    } // while
                }// if

            }//getcontacts

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(v==contacts){
                Intent intent = new Intent();
                intent.setClassName("com.a.Activities",
                        "com.a.Activities.DialPad");
                startActivity(intent);
                finish();
                }
                }

     }// class
        .................................

        public class listviewadapterultimate extends BaseAdapter {
            viewHolder holder;

            Activity context;
            String[] names;
            String[] types;
            String[] duration;
            int[] uniqueid;


            public listviewadapterultimate(Activity context, String[] names,
                    String[] types, int[] uniqueid2 ) {
                this.context = context;
                this.names = names;
                this.types = types;

                uniqueid=uniqueid2;

            }




            public int getCount() {
                // TODO Auto-generated method stub
                return names.length;
            }

            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }

            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            public class viewHolder {
                TextView top;
                TextView bottom;

                TextView down;


            }

            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub



                if (convertView == null) {
                    holder = new viewHolder();
                    LayoutInflater inflator = context.getLayoutInflater();
                    convertView = inflator.inflate(R.layout.recenttextviewonlyultimate, null);
                   holder.top = (TextView) convertView.findViewById(R.id.toptext_u);
                    holder.bottom = (TextView) convertView
                            .findViewById(R.id.bottomtext_u);

                holder.down = (TextView) convertView.findViewById(R.id.recentuniqueid_u);

                convertView.setTag(holder);

                } else {
                    holder = (viewHolder) convertView.getTag();
                    //holder.cb.setVisibility(View.VISIBLE);
                }

                holder.top.setText(names[position]);
                holder.bottom.setText(types[position]);

                holder.down.setText("" + uniqueid[position]);


                return convertView;
            }

        }
        ................

2 个答案:

答案 0 :(得分:1)

试试这个:

在getView(...)方法中......

 final CheckBox lChk = ((CheckBox) pConvertView.findViewById(R.id.myChkBoxID)); 
 private List<lisInfo> m_lisInfo = new ArrayList<lisInfo>();

 lChk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     // Populate the listInfo with check box status
     m_lisInfo.get(lPosition).setChkBoxStatus((isChecked));
  }
 });


 public class lisInfo{
   private boolean chkBoxStatus;

   public boolean isChkBoxStatus() {
        return chkBoxStatus;
    }

    public void setChkBoxStatus(boolean chkBoxStatus) {
        this.chkBoxStatus = chkBoxStatus;
    }
}

现在iterate the listInfo,只需要在列表视图中获取复选框状态

答案 1 :(得分:0)

在适配器内维护一个布尔数组。在getview中设置ckeckbox上的监听器,它将在check / uncheck上刷过数组的值。

现在可以在按钮

的活动中访问此数组
click()
{
for(int i=0;i<array.size;i++)
{
if(array[i])
adapter.deelet(item i);

//modify syntax
}
}