如图所看到的:

我点击Item,右边的checkBox就会对应的变化。可是当我第一次做的时候。点击第一个Item,右边的checkBox变为绿色,可是当我listView往下拉的时候,发现以下也有是绿色的checkBox,非常显然我是没有点击以下的。那么这个问题应该怎么解决呢。以下是我解决办法:

首先是Item的布局:

<?

xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray" > <RelativeLayout
android:id="@+id/update_rela"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:background="@drawable/set_edge_bg" > <ImageView
android:id="@+id/record_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/small_margin"
android:background="@drawable/record_icon2" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/record_icon"
android:orientation="vertical" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/small_margin"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time"
android:textColor="@color/black_gray" /> <TextView
android:id="@+id/tv_synctime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2014年9月19日 17:38:03"
android:textColor="@color/black_gray" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/local"
android:textColor="@color/black_gray" /> <TextView
android:id="@+id/tv_synccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="双向同步完毕。共165条数据"
android:textColor="@color/black_gray" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/local"
android:textColor="@color/black_gray" /> <TextView
android:id="@+id/tv_synclocal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增0,删0,改0"
android:textColor="@color/black_gray" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clouds"
android:textColor="@color/black_gray" /> <TextView
android:id="@+id/tv_syncserver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增0,删0,改0"
android:textColor="@color/black_gray" />
</LinearLayout> </LinearLayout>
</RelativeLayout> <CheckBox
android:id="@+id/update_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:clickable="false"
android:layout_gravity="center_vertical"
android:button="@drawable/checkbox_selector"
android:layout_marginLeft="@dimen/small_margin"
/> </LinearLayout>

checkBox的背景是在res/drawable下的自己定义的checkbox_selector.xml中的

<?

xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/check_on" /><!--选中时效果-->
<item android:state_checked="false"
android:drawable="@drawable/check_off" /><!--未选中时效果-->
<!-- 改动成你自己的图片就能够了 -->
</selector>

以下的是重点,继承的baseAdapter中的getView方法,我已经加了凝视,自己能看明确的

package com.bcinfo.pwzs.ui.adapter;

import java.util.List;

import com.bcinfo.pwzs.R;
import com.bcinfo.pwzs.bean.SyncLog;
import com.bcinfo.pwzs.ui.adapter.UpdateRecordAdapter.Temple; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView; public class UpdateRecordEditAdapter extends BaseAdapter {
LayoutInflater inflater;
List<SyncLog> list; //这里定义一个数组。来识别checkBox是否被选中。<span style="font-family: Arial, Helvetica, sans-serif;">假设在不同包下。</span><span style="font-family: Arial, Helvetica, sans-serif;">记得这里是pubic。</span>
public int first[]; public UpdateRecordEditAdapter(Context context, List<SyncLog> log) {
inflater = LayoutInflater.from(context);
this.list = log;
//这里初始化数组
first=new int[log.size()];
for (int i = 0; i < log.size(); i++) {
first[i]=0;
}
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
Temple te;
if (convertView == null) {
te = new Temple();
convertView = inflater.inflate(R.layout.listview_update_edit, null);
te.synccount = (TextView) convertView
.findViewById(R.id.tv_synccount);
te.synctime = (TextView) convertView.findViewById(R.id.tv_synctime);
te.synclocal = (TextView) convertView
.findViewById(R.id.tv_synclocal);
te.syncserver = (TextView) convertView
.findViewById(R.id.tv_syncserver);
te.cb=(CheckBox) convertView.findViewById(R.id.update_delete);
convertView.setTag(te);
} else {
te = (Temple) convertView.getTag();
}
if (list != null && list.size() != 0) {
te.synccount.setText(list.get(position).getCount());
te.synclocal.setText(list.get(position).getLocal());
te.syncserver.setText(list.get(position).getSever());
te.synctime.setText(list.get(position).getSyncTime());
}
//在这里进行推断。假设是0。代表没被选中,假设是1,代表被选中
if(first[position]==0){
te.cb.setChecked(false);
}else{
te.cb.setChecked(true);
} return convertView; } class Temple {
TextView synctime;
TextView synclocal;
TextView syncserver;
TextView synccount;
CheckBox cb;
} }

接下来就是Activity里面进行操作了,由于Activity里面我写的东西比較多。不能所有复制过来,我把详细实现代码贴出来

listView的点击事件中那段凝视以下是重点

//定义的adapter
UpdateRecordEditAdapter adapter;
//定义的listView
ListView listview; //以下是listView的点击事件
listview.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) { CheckBox cb = (CheckBox) view.findViewById(R.id.update_delete);
if (cb.isChecked()) {
cb.setChecked(false); } else {
cb.setChecked(true); }
//在这里进行改变adapter里面的first数组中的值,这里是重点哈
if(adapter.first[position]==0){
adapter.first[position]=1;
}else{
adapter.first[position]=0;
} }
});

解决“listView点击一个Item,另外几个Item也跟着改变”的问题的更多相关文章

  1. 解决listview点击item失效

    开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了 ...

  2. android中listview的item滑动删除效果(已解决listview点击问题)

    领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...

  3. android 解决ListView点击与滑动事件冲突

    如果你的ListView的Item有滑动功能,但又点击Item跳转到其它activity,这样若是在Adapter里面写点击事件是会导致滑动事件获取不到焦点而失效: 解决方法:不要在adapter里面 ...

  4. ListView点击事件失效(item里面有button按钮控件)解决方法

    ListView点击事件失效解决方法: 一般出现这个情况,就是你的item里面有按钮的点击事件,你的item里面有button控件,button控件是抢占焦点的,只要在你的item布局里面这样子写就可 ...

  5. 解决ListView中Item的子控件与Item点击事件冲突

    常常会碰到在ListView中点击当中一个Item.会一并触发其子控件的点击事件.比如Item中的Button.ImageButton等.导致了点击Item中Button以外区域也会触发Button点 ...

  6. Android中Listview点击item不变颜色以及设置listselector 无效

    Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...

  7. SlideAndDragListView,一个可排序可滑动item的ListView

    SlideAndDragListView简介 SlideAndDragListView,可排序.可滑动item显示"菜单"的ListView. SlideAndDragListVi ...

  8. Android ListView的item背景色设置以及item点击无响应等相关问题

    Android ListView的item背景色设置以及item点击无响应等相关问题 在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的ite ...

  9. ListView点击Item展开隐藏项(单项展开、多项展开、复杂布局时的展开处理)

    手机屏幕毕竟有限,当我们要显示较多数据时便不得不舍去一些次要信息.将主要信息优先显示,也使显示效果更加简洁美观.遇到类似的需求,我们使用最多的就是 ListView ,而假设每次点击一个 Item 都 ...

随机推荐

  1. 一步一步学习IdentityServer3 (13) 令牌

    IdentityServer3中客户端保护了授权资源,不难看出在IdentityServer3中,有这样一个设置 AllowedScopes = new List<string> { &q ...

  2. jenkins远程执行shell

    旧版本: 安装插件 SSH plugin 1. 增加一个domain,点击OK 点击 adding some credentials 填写要远程连接的服务器的用户名和密码(以下例子为连接到91机器的r ...

  3. mysql的第一个程序

    每次写java链接数据怎么写,这一次做一个总结,方便参考. 1.在mysql上下载mysql驱动jar包 2.下载的驱动包 3.程序目录 4.程序 package mysqlTest; import ...

  4. 004 Hadoop2.x基础知识

    一:大数据应用 1.Cloudera cloudera公司是Hadoop三大发行商之一,其版本为CDH版本,现在最新的版本是CDH5. 网站:http://archive.cloudera.com/c ...

  5. 学习 HMM

    简介 HMM 中的变量可以分为两组. 第一组是状态变量 \(\{y_i,y_2,\cdots, y_n\}\), 其中 \(y_i \in \mathcal{Y}\) 表示第 \(i\) 时刻的系统状 ...

  6. Oracle数据库的基本查询

    本文用的是Oracle 10g数据库,利用PL/SQL Developer的集成开发环境(安装可以自行百度) Oracle数据库  ---> 数据库实例  --->  表空间(逻辑单位)( ...

  7. 【GO基础】神奇的iota特殊常量

    最近在学习GO语言,然后发现有一个特殊常量是以前没有接触过的,比较感兴趣,这里给大家介绍一下. iota,特殊常量,可以被认为是一个可以被编译器修改的常量. 核心概念:iota在const关键字出现时 ...

  8. Mybatis 源码分析之事物管理

    Mybatis 提供了事物的顶层接口: public interface Transaction { /** * Retrieve inner database connection * @retur ...

  9. OSI、TCP、IP、UDP 这些都是啥??

    一个大大的问号首先抛出,计算机之间是如何进行通信的? 计算机网络是通过传输介质.通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来,实现资源共享和数据传输的系统. 网络协议就是数据按照一定的 ...

  10. Web2.0应用程序的7条原则

    个人看好Web的发展潜力,本文字摘自<Collective Intelligence 实战> 网络是平台 使用传统许可模式软件的公司或用户必须运行软件.定期更新至最新版本,以及扩展它来满足 ...