Android列表视图每个都有一个复选框

时间:2013-07-05 11:20:00

标签: android

我有一个列表视图,其列表项包括文本视图和复选框。 Textview从数据库中获取其数据。因此,我的列表视图包含多个文本视图,每个文本视图都带有一我的问题是我无法将每个复选框映射到相应的列表项

2 个答案:

答案 0 :(得分:1)

制作类似

的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >


<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
  />


<CheckBox

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />



  </LinearLayout >

在自定义适配器中使用它 自定义适配器:

    public class AttendeeAdapter extends ArrayAdapter<String> {

private LayoutInflater inflator = null;
private ArrayList<String> attendeeList = null;

public AttendeeAdapter(Context context, int resource,
        List<String> objects) {
    super(context, resource, objects);

    attendeeList = (ArrayList<String>) objects;
    inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if(convertView == null) {
        convertView = inflator.inflate(R.layout.client_row, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
        holder.txtViewName = (TextView) convertView.findViewById(R.id.txtViewClientName);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    String attendee = attendeeList.get(position);

    holder.txtViewName.setText("name");
    return  convertView;
}

private static class ViewHolder {
    public TextView txtViewName = null;
            public CheckBox check= null;
}

 }

R.layout.client_row是自定义布局,其中包含textview和复选框

答案 1 :(得分:0)

使用 ViewHolder 类来绑定列表视图。

以下是视图持有者m用于在listview

中的每一行设置朋友姓名和复选框的示例

// Array Adapter在Listview上显示好友名称

                  public class FriendAdapter extends ArrayAdapter<BinforFacebook>
                  {
                      ImageLoader loader=new ImageLoader(getApplicationContext());
                      Bitmap res;
                      String url;
                      ArrayList<BinforFacebook> namelist = new ArrayList<BinforFacebook>(); 

                      public FriendAdapter(Context context,int textViewResourceId,ArrayList<BinforFacebook> objects)
                        {
                            super(getApplicationContext(), textViewResourceId, objects);
                            this.namelist=objects;
                            // TODO Auto-generated constructor stub
                        }

                      @Override
                        public View getView(final int position, View convertView,
                                ViewGroup parent) {
                            // TODO Auto-generated method stub
                          ContactsViewHolder viewHolder;

                        //  loader.clearCache();

                          Log.v("in get View","ger View of new create miline");

                          if (convertView == null) {

                                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                convertView = inflater.inflate(R.layout.friendsnameadd, parent, false);
                                viewHolder = new ContactsViewHolder();
                                viewHolder.txtname = (TextView) convertView.findViewById(R.id.name_contact);
                                viewHolder.txtmilineuptag = (TextView) convertView.findViewById(R.id.tagmilineup);
                                viewHolder.chkmiteam = (CheckBox) convertView.findViewById(R.id.Checkmilineup);
                                viewHolder.frndimg = (ImageView) convertView.findViewById(R.id.frndimg);

                              //adding facebook friend id into list to store in database and to post image to their wall  

                                viewHolder.chkmiteam.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                                    @Override
                                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                        // TODO Auto-generated method stub
                                          int getPosition = (Integer) buttonView.getTag(); 

                                          namelist.get(getPosition).setFlag((buttonView.isChecked())); // Set the value of checkbox to maintain its state.
                                          if(isChecked)
                                          {
                                              Log.v("namelist id",namelist.get(getPosition).getFbid().toString());
                                                 if(!fbidlist_todb.contains(namelist.get(getPosition).getFbid().toString()))
                                                 {
                                                     fbidlist_todb.add(namelist.get(getPosition).getFbid().toString());
                                                 }
                                                  else
                                                  {
                                                      fbidlist_todb.remove(namelist.get(getPosition).getFbid());
                                                  }
                                          }
                                    }
                                });

                                convertView.setTag(viewHolder);
                                convertView.setTag(R.id.name_contact, viewHolder.txtname);
                                convertView.setTag(R.id.tagmilineup, viewHolder.txtmilineuptag);
                                convertView.setTag(R.id.Checkmilineup, viewHolder.chkmiteam);
                                convertView.setTag(R.id.frndimg, viewHolder.frndimg);
                            }
                            else {
                                viewHolder = (ContactsViewHolder) convertView.getTag();
                            }

                            viewHolder.chkmiteam.setTag(position); // This line is important.  
                            viewHolder.txtname.setText(namelist.get(position).getFbname());
                            viewHolder.chkmiteam.setChecked(namelist.get(position).flag);

                            url=(namelist.get(position).getUrl());
                            Log.v("url in getview",url);
                      //      viewHolder.frndimg.setImageBitmap(namelist.get(position).getB1());
                            loader.DisplayImage(url,viewHolder.frndimg);

                            /*checking if current position friend is already added in to team or not.
                             * if added then make checkbox checked
                             */

                            if(namelist.get(position).getFbid()!=null)
                            {
                                Log.v("fb name list uid",namelist.get(position).getFbid());
                                 for(int i=0;i<fbidlist_fromdb.size();i++)
                                    {
                                        if(namelist.get(position).getFbid().equals(fbidlist_fromdb.get(i).toString()))
                                        {
                                            viewHolder.chkmiteam.setChecked(true);
                                        }
                                    }
                            }

                          return convertView;
                        }
                  }