如何在列表视图项中添加复选框?

发布于 2024-12-11 16:12:03 字数 2458 浏览 0 评论 0原文

在我的应用程序中,我在一个活动中使用两个列表视图,一个仅用于项目,一个列表视图我在每一行中添加复选框。 我的要求是在下面的列表视图中,如果选中项目,我必须在主要活动的文本视图上显示(如果不需要显示其他活动)。

我没有为此使用 ListActivity 我只是从 Activity 创建

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout 
    android:orientation="vertical"
    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="60px">                
    <ListView 
        android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_weight="0.5"
        android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout 
    android:orientation="vertical"
    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="60px">

    <ListView android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_weight="0.5"
        android:layout_height="wrap_content" />
</LinearLayout>    

上面的列表视图仅适用于下面的列表视图的项目,我必须添加复选框。我怎样才能做到这一点。

我的活动是..

public  class Settings extends Activity   {    
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.settings); 

   ListView list1 = (ListView) findViewById(R.id.list1);
   ListView list2 = (ListView) findViewById(R.id.list2);

   list2.setAdapter(new ArrayAdapter(this,      
     android.R.layout.simple_expandable_list_item_1, Display));

   list2.setTextFilterEnabled(true);
   list2.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

      }
 });    
   list1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, Categories));

   list1.setTextFilterEnabled(true);
   list1.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

      }
 });                
}
    static final String[] Categories = new String[] {
    "Alarm1","Alarm2","Alarm3","Alarm4","Alarm5"
    };
    static final String[] Display = new String[] {
    "24 Hours","Display Seconds"
    };   
 }

In My application i am using two list view's in one activity, one for only items and one list view i am have add check box in every row.
My requirement is in below list view if item is checked i have to display on text view in the main activity if not have to display another activity .

i was not used ListActivity for this i just created from Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout 
    android:orientation="vertical"
    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="60px">                
    <ListView 
        android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_weight="0.5"
        android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout 
    android:orientation="vertical"
    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="60px">

    <ListView android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_weight="0.5"
        android:layout_height="wrap_content" />
</LinearLayout>    

above list view for only items for below list view i have to add check box. how can i achieve this.

my activity is..

public  class Settings extends Activity   {    
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.settings); 

   ListView list1 = (ListView) findViewById(R.id.list1);
   ListView list2 = (ListView) findViewById(R.id.list2);

   list2.setAdapter(new ArrayAdapter(this,      
     android.R.layout.simple_expandable_list_item_1, Display));

   list2.setTextFilterEnabled(true);
   list2.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

      }
 });    
   list1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, Categories));

   list1.setTextFilterEnabled(true);
   list1.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

      }
 });                
}
    static final String[] Categories = new String[] {
    "Alarm1","Alarm2","Alarm3","Alarm4","Alarm5"
    };
    static final String[] Display = new String[] {
    "24 Hours","Display Seconds"
    };   
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是我以为 2024-12-18 16:12:03
@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = mInflater.inflate(R.layout.provider, null);
            TextView location = (TextView) convertView
                    .findViewById(R.id.description);
            checkBoxImage = (ImageView) convertView.findViewById(R.id.check_id);
            Provider p = (Provider) getItem(position);
            location.setText(p.Description + " - " + p.Location);
            return convertView;
        }
@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = mInflater.inflate(R.layout.provider, null);
            TextView location = (TextView) convertView
                    .findViewById(R.id.description);
            checkBoxImage = (ImageView) convertView.findViewById(R.id.check_id);
            Provider p = (Provider) getItem(position);
            location.setText(p.Description + " - " + p.Location);
            return convertView;
        }
不喜欢何必死缠烂打 2024-12-18 16:12:03

那么这里是如何在列表中包含复选框的完整教程。这是关于 ListView 的最佳教程列表活动

Well here is the Complete Tutorial how you can include CheckBox in a List. Its the Best Tutorial Regarding the ListView & ListActivity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文