如图所看到的:

我点击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. Java深度复制List内容。

    最近在工作的时候,有一个小需求,需要复制List的内容,然后会改变其中的数据,但是试了几种复制的方法,都是将原有的数据和复制后的数据都改变了,都没有达到我想要的效果. 其中涉及到了 "浅复制 ...

  2. linux 101 hacks 2date,grep,find

    感觉挨个按着作者来的太蠢了,我还是放自己觉得不错的东西把 用特定格式显示当前时间 以下的方法可以用各种不同的格式来显示当前时间: $ date Thu Jan :: PST $ date --date ...

  3. 编写一个简单的 JDBC 程序

    连接数据库的步骤: 1.注册驱动(只做一次) 2.建立连接(Connection) 3.创建执行SQL的语句(Statement) 4.执行语句 5.处理执行结果(ResultSet) 6.释放资源 ...

  4. Zookeeper服务器集群的搭建与操作

    ZooKeeper 作用:Zookeeper 可以用来保证数据在zk集群之间的数据的事务性一致(原子操作). 介绍:Zookeeper 是 Google 的 Chubby一个开源的实现,是 Hadoo ...

  5. iOS开发之app打包发布流程

    一.准备工作 苹果开发者中心 1.申请苹果开发者账号 首先需要申请苹果开发者账号才能在APP store 里发布应用. 开发者账号分类:(1)个人开发者账号 (2)企业开发者账号 主要的区别是:点击这 ...

  6. 微信JS-SDK之图像接口开发详解

    由于现在手头的项目中有一个上传证件照认证的功能(手机端),之前的思路是直接点击上传,然后直接将图片上传到服务器去,这篇文章有讲到(http://www.cnblogs.com/it-cen/p/453 ...

  7. 解决命令行运行python文件,出现No module named *** 报错问题

    有时候在一个项目中运行的时候,可能是之前已经mark成sources root 你自己忘记了, 于是就在命令行也执行python文件,然后就出现 No module named *** 等 相关你认为 ...

  8. POJ 2260 Error Correction 模拟 贪心 简单题

    Error Correction Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6825   Accepted: 4289 ...

  9. 不定期更新的IDEA功能整理

    目录 不定期更新的IDEA功能整理 idea 命令 Preferences 和 Project Structure Keymap HTTP Proxy Postfix Completion 插件 插件 ...

  10. CSS3组件化之菊花loading

    <div class="juhua-loading"> <div class="jh-circle"></div> < ...