如图所看到的:

我点击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. RCTF2015 pwn试题分析

    pwn200 漏洞给的很明显,先是读到了main的局部数组中,然后在子函数中向子函数的局部数组栈里复制. 总体思路是leak system的地址,然后再向一个固定地址写入/bin/sh,最后执行sys ...

  2. day4作业模拟实现一个ATM + 购物商城程序

    作业需求: 模拟实现一个ATM + 购物商城程序 1.额度 15000或自定义: 2.实现购物商城,买东西加入 购物车,调用信用卡接口结账: 3.可以提现,手续费5%: 4.每月22号出账单,每月10 ...

  3. Ngram折扣平滑算法

    本文档翻译自srilm手册ngram-discount.7.html     NAME ngram-discount – 这里主要说明srilm中实现的平滑算法   NOTATION a_z      ...

  4. gitlab-针对API,获取私有令牌

    Gitlab有一个强大的API系统,几乎所有的功能都可以在web中执行,当然也可以通过API来执行,为了使用API,需要从Gitlab中获取私有token. 执行步骤: 1. 登陆Gitlab服务器 ...

  5. JS学习笔记(一)DOM事件和监听

    将事件绑定到元素身上的三种方法: 1.HTML事件处理程序(不推荐使用) 1 <a onclick="hide()"> 2.传统的DOM事件处理程序 即在目标DOM事件 ...

  6. Asp.Net Core2.0允许跨域请求设置

    1.services /// <summary> /// /// </summary> /// <param name="services">& ...

  7. sqoop遇到的问题

    我使用的是CDH版本的 这是我的sqoop脚本 sudo -u hive sqoop import --connect jdbc:mysql://xxxx/rom3 --username xxx -- ...

  8. CSUOJ 1271 Brackets Sequence 括号匹配

    Description ]. Output For each test case, print how many places there are, into which you insert a ' ...

  9. oneDay

    难受过 迷茫过 失望过 耍脾气过 开心过 伤心过 疼过 走了这么久的路: 我只想说 程序的道路上 很难走: 本来准备都放弃了: 自己逼自己了很久想明白了: 不能什么时候都想着靠外力 自己的不足就是自己 ...

  10. Xamarin 2017.11.1更新

     Xamarin 2017.11.1更新 本次更新主要解决了一些bug.Visual Studio 2017升级到15.4.2获得新功能.Visual Studio 2015需要工具-选项-Xamar ...