`
20386053
  • 浏览: 461902 次
文章分类
社区版块
存档分类
最新评论

ListView中CheckBox刷新消失解决方案二

 
阅读更多

之前那篇博客的那个点击单个checkbox刷新还是会消失

这次开发中再次用到,优化了下,其实不难就是开发中经常接触这种组件组合

首先主Activity:

package com.listviewtest;

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListViewTest extends ListActivity {
	
	/** Called when the activity is first created. */  
    Context context = null;   
    List<Integer> mList = new ArrayList<Integer>();
    CheckBox checkbox;
	private RelativeLayout mRelativeLayout;
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
        context = getApplicationContext();   
        setListAdapter(new MyListAdapter());   
    }   
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mRelativeLayout = (RelativeLayout) v;
		checkbox = (CheckBox) mRelativeLayout.getChildAt(1);
		if (mList.contains(new Integer(position))) {
			checkbox.setChecked(false);
			mList.remove(new Integer(position));
		}else {
			checkbox.setChecked(true);
			mList.add(new Integer(position));
		}
	}
    
	class MyListAdapter extends BaseAdapter{   
        String data[] = new String[]{"11111", "22222", "33333", "44444","55555", "66666", "77777", "88888","99999", "10101", "12121", "13131"};   
        public int getCount() {   
            return data.length;   
        }   
  
        public Object getItem(int position) {   
            return data[position];   
        }   
  
        public long getItemId(int position) {   
            return position;   
        }   
  
        public View getView(int position, View convertView, ViewGroup parent) {   
            if (null == convertView){   
                LayoutInflater inflater      = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
                convertView = inflater.inflate(R.layout.list_item, parent, false);   
            }   
            TextView text       =   (TextView)convertView.findViewById(R.id.info);   
            checkbox   =   (CheckBox)convertView.findViewById(R.id.checkstatus);   
            checkbox.setTag(new Integer(position));   
            text.setText(data[position]);   
            if (mList != null) {
            	checkbox.setChecked((mList.contains(new Integer(position)))); 
			} else {
				checkbox.setChecked(false);   
			}
            return convertView;   
        }   
    }  
}


其次是main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <ListView       
        android:id ="@android:id/list"    
        android:layout_width="wrap_content"    
        android:layout_height="fill_parent"  
        android:scrollbarStyle="outsideOverlay"
        android:cacheColorHint="#00000000"
        android:fadingEdge="none"/>  
</LinearLayout>  


还有list_item.xml

<?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"  
    android:orientation="horizontal">  
    <TextView  
        android:id="@+id/info" android:layout_width="wrap_content"  
        android:layout_height="fill_parent" android:layout_alignParentLeft="true"
        android:paddingLeft="5dip"/>  
        
    <CheckBox  
        android:id="@+id/checkstatus" android:layout_width="wrap_content"  
        android:layout_height="wrap_content" android:focusable="false" 
        android:clickable="false" android:paddingRight="10dip"
        android:layout_alignParentRight="true"/>  
</RelativeLayout>  


分享到:
评论

相关推荐

    ListView的CheckBox错位Demo

    在"ListViewDemo"项目中,开发者可能已经实现了一个类似的解决方案。分析这个Demo,我们可以学习如何处理ListView中CheckBox的错位问题,同时理解Adapter的复用机制和事件监听的使用。 总结起来,解决ListView中...

    listview嵌套checkbox

    二、ListView嵌套CheckBox的问题及解决方案 1. 选中状态同步:由于ListView的重用机制,当滑动列表时,CheckBox的选中状态可能会出现混乱。为解决此问题,需要在getView()方法中设置CheckBox的状态,根据数据模型的...

    自定义ListView加CheckBox

    - 解决方案是,在`getView()`中,根据当前的数据项状态设置CheckBox的选中状态,而不是依赖于convertView的状态。 6. **点击事件处理** - 为ListView的每一项设置点击事件监听器,当点击发生时,更新数据模型中的...

    完美解决Listview嵌套Checkbox滑动后位置错乱问题,和checkbox的全选或全不选。

    以上就是解决ListView中Checkbox滑动后位置错乱问题和实现全选/全不选功能的完整方案。通过合理利用ViewHolder和监听CheckBox状态,我们可以确保每个Checkbox的状态在滑动时得到正确维护,同时提供方便的全选操作。...

    ListView+CheckBox

    最后,关于“ListViewCheckBoxTest”这个文件名,可能是一个测试项目或者示例代码,它可能包含了上述解决方案的实现,包括自定义适配器、数据模型以及处理CheckBox事件的相关代码。通过运行和调试这个测试项目,...

    Android防京东筛选,ListView+CheckBox滑动冲突

    本文将详细探讨这些问题,并提供解决方案。 首先,我们要解决的是"ListView+CheckBox滑动冲突"的问题。在ListView中,每个Item可能包含一个或多个可交互元素,如CheckBox。当用户尝试滑动ListView时,如果手指不...

    android中adapter与checkbox、editText冲突的解决方案

    2. 数据同步问题:当用户在EditText中输入内容后,数据并未实时更新到数据源,导致刷新ListView时,EditText中的值未保存。这是因为在EditText的文本改变监听器中没有正确地更新数据源。解决方案是在监听器中获取...

    checkbox混乱的解决

    然而,在实现ListView时,我们可能会遇到一个常见的问题,那就是“checkbox混乱”,这通常发生在用户交互时,如点击或勾选ListView中的复选框(checkbox)。这个问题的核心在于ListView的复用机制,导致了复选状态的...

    AppWidget:一些高级android appwidget属性,例如listview,复选框(使用imagebutton实现),刷新数据运行时,使用shared_prefs保持kepp状态

    AppWidget 显示一些高级android appwidget属性,例如listview,复选框(使用imagebutton实现),刷新数据运行时以及使用shared_prefs保持状态。...这里提供了一种实现listview中checkbox效果的解决方案。

    Android 中ListView点击Item无响应问题的解决办法

    然而,在某些情况下,当ListView的Item中包含可交互的控件,如Button或Checkbox时,可能会遇到点击Item无响应的问题。这是因为这些控件会捕获并保持焦点,使得ListView本身无法接收到点击事件。为了解决这个问题,...

    MoreChooseList.zip

    本文将深入探讨这个问题,并提供解决方案。 “错位问题”通常发生在ListView或者RecyclerView等滚动视图中,当用户快速滚动列表时,某些已选择的项目可能显示在它们不应该出现的位置上。这是由于Android为了提高...

    vfp6.0系统免费下载

    问题 2-7: 我的 Visual FoxPro 5.0 表单中包含有 ListView 或 TreeView 控件,它们不继承其新版本中的最新功能(如支持复选框)。已发布的应用程序中,由于带有这些表单,而在最终用户的机器上出现表单 OLE 错误,...

    网格布局的GridView多选删除功能

    - "源码说明.htm"可能提供了关于源码的详细解释,包括如何集成到自己的项目中,以及可能遇到的问题和解决方案。 - "更多海量源码下载.url"可能链接到一个资源库,提供了更多的Android开发示例代码,对于学习和提高...

    免费DataGridView打印及.NET轻松打印控件5.0版

    1、解决了生成预览及打印过程中的状态窗口中显示的“第 页共 页”问题,现在可以正确显示总页数。 2、增加了打印DataGridView中选定内容(选定行、列或矩形区域)的功能。对DGVPrint组件,设置PrintRange属性为相应...

    史上最好传智播客就业班.net培训教程60G 不下会后悔

    再比如ASP.Net内置的AJAX解决方案UpdatePanel只在部分要求不高的内网项目中才被使用,因此我们在讲解UpdatePanel的使用和原理之外,把更多的时间放在讲解企业中用的最多的JQuery AJAX解决方案上。 6、B/S系统项目(7...

    C#编程经验技巧宝典

    2 &lt;br&gt;0003 设置程序代码行序号 3 &lt;br&gt;0004 开发环境全屏显示 3 &lt;br&gt;0005 设置窗口的自动隐藏功能 3 &lt;br&gt;0006 根据需要创建所需解决方案 4 &lt;br&gt;0007 如何使用“验证的目标架构”功能 4 ...

    ASP.NET 3.5 开发大全word课件

    1.3.5 解决方案管理器 1.3.6 属性窗口 1.3.7 错误列表窗口 1.4 安装SQL Server 2005 1.5 ASP.NET应用程序基础 1.5.1 创建ASP.NET应用程序 1.5.2 运行ASP.NET应用程序 1.5.3 编译ASP.NET应用程序 1.6 小结 第2章 C# ...

    ASP.NET3.5从入门到精通

    1.3.5 解决方案管理器 1.3.6 属性窗口 1.3.7 错误列表窗口 1.4 安装SQL Server 2005 1.5 ASP.NET 应用程序基础 1.5.1 创建ASP.NET 应用程序 1.5.2 运行ASP.NET 应用程序 1.5.3 编译ASP.NET 应用程序 1.6 小结 第 2 ...

    ASP.NET 3.5 开发大全11-15

    1.3.5 解决方案管理器 1.3.6 属性窗口 1.3.7 错误列表窗口 1.4 安装SQL Server 2005 1.5 ASP.NET应用程序基础 1.5.1 创建ASP.NET应用程序 1.5.2 运行ASP.NET应用程序 1.5.3 编译ASP.NET应用程序 1.6 小结 第2章 C# ...

    ASP.NET 3.5 开发大全

    1.3.5 解决方案管理器 1.3.6 属性窗口 1.3.7 错误列表窗口 1.4 安装SQL Server 2005 1.5 ASP.NET应用程序基础 1.5.1 创建ASP.NET应用程序 1.5.2 运行ASP.NET应用程序 1.5.3 编译ASP.NET应用程序 1.6 小结 第2章 C# ...

Global site tag (gtag.js) - Google Analytics