【问题标题】:listview with checkbox带复选框的列表视图
【发布时间】:2011-03-21 10:00:37
【问题描述】:

我想显示一个带有复选框的列表视图

          checkbox listitem1
          checkbox listitem2
          checkbox listitem3
                  .
                  .
                  .
                  .

如果单击列表视图中的任何列表项,则选中的相应复选框将为 true 。我试过下面的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox android:text=""
    android:id="@+id/list_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    ></CheckBox>
 <TextView
 android:id="@+id/songname"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:layout_marginTop="10px"
 android:layout_marginLeft="60px"/>
 <TextView
 android:id="@+id/artist"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:layout_marginTop="30px"
 android:layout_marginLeft="60px"/>
</RelativeLayout> 

类文件是

 list.setOnItemClickListener(this);
 list.setAdapter(new EfficientAdapter(this));

private static class EfficientAdapter extends BaseAdapter 
    {
       private LayoutInflater mInflater;

       public EfficientAdapter(Context context) 
       {
       mInflater = LayoutInflater.from(context);
       }

       public int getCount() 
       {
       return title.length;
       }

       public Object getItem(int position) 
       {
       return position;
       }

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

       public View getView(int position, View convertView, ViewGroup parent) 
       {
       ViewHolder holder;
       if (convertView == null) 
       {
       convertView = mInflater.inflate(R.layout.selectsongs, null);
       holder = new ViewHolder();

       holder.title = (TextView) convertView.findViewById(R.id.songname);
       holder.artist = (TextView) convertView.findViewById(R.id.artist);
       holder.check = (CheckBox) convertView.findViewById(R.id.list_checkbox);    
       convertView.setTag(holder);
       } 
       else 
       {
       holder = (ViewHolder) convertView.getTag();
       }

       holder.title.setText(title[position]);  
       holder.artist.setText(artist[position]);

       return convertView;
       }

       static class ViewHolder 
       {
       TextView title,artist;

       CheckBox check;

       }
       }
    @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    // TODO Auto-generated method stub
    Log.e("name",title[position]);

}

但在此列表视图上的 OnClickItemClickListener 不起作用。当我单击不在列表视图中的列表项上的复选框时,可选中的复选框为真。所以请告诉我如何显示带有复选框的列表视图,并且当我单击列表项时,列表项复选框可检查为真。

最好的问候。

提前致谢

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

    最好使用ChechBoxPreference 完成列表。

    使用首选项的主要优点是您无需编写代码来保存该值,并且您可以在任何活动中轻松获取该值。该值作为密钥对值存储在 android 首选项中。您可以使用“KeyName”引用该值。

    以下链接将帮助您对此有所了解:

    http://geekswithblogs.net/bosuch/archive/2010/12/03/android---creating-a-custom-preferences-activity-screen.aspx

    【讨论】:

      【解决方案2】:

      我也有同样的问题。我两天前刚刚修好了:

      在:公共视图getView中

       cb =(CheckBox)row.findViewById(R.id.CheckBox01);
                cb.setChecked(etat[position]);
      
                final int xt=position;
      
                cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                  @Override
                  public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                      if(arg0.isChecked())
                      {           
                          etat[xt]=true;
                          //Update the state of Checkbox to the: "tabEtat"
                          Etat.getInstance().setAddIdEtat(String.valueOf(xt));
                              }                         
                      }
                      else
                      {
                          //Update the state of Checkbox to the : "tabEtat"
                          Etat.getInstance().setDeleteIdEtat(String.valueOf(xt));
                              }                          
                      }
                  }
              });       
      

      【讨论】:

        【解决方案3】:

        经过两个小时的努力,我得到了解决方案。

        /**
        *This is Adapter class 
        */
          public class CustomListAdapter extends BaseAdapter {
        
                private String[] stringArray;
                private Context mContext;
                private LayoutInflater inflator;
                int checkbox;
                /**
                 * 
                 * @param context
                 * @param stringArray
                 */
                public CustomListAdapter(Context  context, String[] stringArray) 
                {
                    this.mContext=context;
                    this.stringArray=stringArray;
                    this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
                }
        
                @Override
                public int getCount()
                {
                    return stringArray.length;
                }
        
                @Override
                public Object getItem(int position)
                {
                    return position;
                }
        
                @Override
                public long getItemId(int position)
                {
                    return position;
                }
        
                @Override
                public View getView(int position, View convertView, ViewGroup parent)
                {
        
                    final MainListHolder mHolder;
                    View v = convertView;
                    if (convertView == null)
                    {
                        mHolder = new MainListHolder();
                        v = inflator.inflate(android.R.layout.simple_list_item_multiple_choice, null);
                        mHolder.txt=(CheckedTextView) v.findViewById(android.R.id.text1);
                        v.setTag(mHolder);
                    } 
                    else
                    {
                        mHolder = (MainListHolder) v.getTag();
                    }
                    mHolder.txt.setText(stringArray[position]);
                    mHolder.txt.setTextSize(12);
                    mHolder.txt.setTextColor(Color.YELLOW);
        
                /**
                 * When checkbox image is set By setImageFromResourceCheckBox(int id) method...Otherwise it takes default
                 */
                    if(checkbox!=0)
                        mHolder.txt.setCheckMarkDrawable(R.drawable.android_button);
        
                    mHolder.txt.setPadding(5, 5, 5, 5);
                    return v;
                }
                class MainListHolder 
                {
                    private CheckedTextView txt;
        
                }
                /***
                 * Setting Image for Checkbox
                 * @param id
                 * 
                 */
                public void setImageFromResourceCheckBox(int id)
                {
                    this.checkbox=id;
                }
        
        
            }
        

        而Activity类应该是这样的。

        public class MyActivity extends ListActivity implements OnItemClickListener  {
        
        
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        
        
        
                CustomListAdapter c=new CustomListAdapter(this,GENRES);
              //  c.setImageForCheckBox(R.drawable.android_button);//Image for checkbox
                setListAdapter(c);
        
                final ListView listView = getListView();
                listView.setItemsCanFocus(false);
              //  listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// For single mOde
        
                listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                listView.setItemChecked(2, true);
        
                listView.setOnItemClickListener(this);
            }
        
        
            private static  String[] GENRES = new String[] {
                "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
                "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
            };
            @Override
            public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
            {
        
            SparseBooleanArray sp=getListView().getCheckedItemPositions();
        
            String str="";
            for(int i=0;i<sp.size();i++)
            {
                str+=GENRES[sp.keyAt(i)]+",";
            }
            Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
        
            }
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2012-03-09
          • 1970-01-01
          • 1970-01-01
          • 2016-03-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-14
          • 1970-01-01
          相关资源
          最近更新 更多