列表视图使用复选框 [英] listview with checkbox

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

问题描述

我要显示与复选框列表视图像

i want to show a listview with check box like

          checkbox listitem1
          checkbox listitem2
          checkbox listitem3
                  .
                  .
                  .
                  .

如果点击列表视图中的任何列表项,然后选中相应的复选框将是真实的。我想下面code

If click on any listitem in listview then the corresponding check box checked will be true . I tried below code

<?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上的列表视图不工作。该复选框可检查的是真实的,当我在列表视图中点击复选框没有列表项。所以,请告诉我如何展示与复选框,并列表项的复选框可检查的是真实的,当我点击列表项。

but in this the OnClickItemClickListener on the listview is not working. The checkbox checkable is true when i click on the check box not on listitem in listview. So please tell me how to show the listview with checkbox and also listitem checkbox checkable is true when i click on the listitem.

最好的问候。

在此先感谢

推荐答案

其更好地使用的 ChechBox preference

使用preference的主要优点是,你不需要写code保存的值,你可以很容易地在任何活动的价值。该值存储在Android preference作为密钥对值。您可以参考使用KeyName是'的值。

The main advantage of using preference is that you don't need to write code to save the value and you can easily get the value in any activity. The value is stored in the android preference as a key-pair value. You can refer the value using the 'KeyName'.

下面的链接将帮助你得到关于这一点的想法:

The following link will help you to get an idea about this:

<一个href="http://geekswithblogs.net/bosuch/archive/2010/12/03/android---creating-a-custom-$p$pferences-activity-screen.aspx" rel="nofollow">http://geekswithblogs.net/bosuch/archive/2010/12/03/android---creating-a-custom-$p$pferences-activity-screen.aspx

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

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