如图所看到的:

我点击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. sublime text配置make工具

    sublime text配置make工具 Linux下许多项目是用makefile来管理的,是用gcc+make等方式来编译和运行. 在只有tty的场合或年代,使用vim或emacs是不二选择:但在L ...

  2. 红黑树:个人理解与Python实现

    红黑树:个人理解与Python实现 [基本事实1] 红黑树是一种平衡的二叉查找树,无论插入还是删除操作都可以在O(lg n)内实现,而一般的二叉查找树则在极端情况下会退化为线性结构.红黑树之所以是平衡 ...

  3. Rookey.Frame之菜单设置

    在上一篇博文 Rookey.Frame企业级快速开发框架开源了 中我们介绍了Rookey.Frame极速开发框架的最新更新及开源介绍,后面慢慢介绍该框架的使用方法,本人文笔不好,写得不够好的地方请大家 ...

  4. vmstat详解

    一.前言 很显然从名字中我们就可以知道vmstat是一个查看虚拟内存(Virtual Memory)使用状况的工具,但是怎样通过vmstat来发现系统中的瓶颈呢?在回答这个问题前,还是让我们回顾一下L ...

  5. 8-13 Just Finish it up uva11093

    题意:环形跑道上有n n<=100000 个加油站  编号为1-n  第i个加油站可以加油pi加仑   从加油站i开到下一站需要qi加仑   你可以选择一个加油站作为起点 初始油箱为空   如果 ...

  6. C# 中使用 Task 实现提前加载

    介绍一种/两种可以提前做点什么事情的方法. 场景 在UI线程中执行耗时操作,如读取大文件,为了不造成UI卡顿,常采用异步加载的方式,即 async/await . 通常的写法是这样的: private ...

  7. CSUOJ 1973 给自己出题的小X DFS

    Description 小X学习了dfs,为了练习搜索,开始给自己出题玩. 玩着玩着,一会把自己难住了,一会又被自己难倒了,真是有趣诶! 小X出的题: 现在有N个不同的正整数,求它们可以组成多少个这样 ...

  8. CSU - 2061 Z‘s Coffee

    Description Z is crazy about coffee. One day he bought three cups of coffee. The first cup has a cap ...

  9. eNSP仿真学习和VLAN配置

    路由&交换机基本命令 sys #切换到系统视图(修改配置),Ctrl+Z 返回用户视图 sysname SW1 #设备重命名为SW1 int g0/0/1 #进入接口视图 VLAN配置 首先连 ...

  10. 论 ArrayList如何实现线程安全

    一:使用synchronized关键字 二:使用Collections.synchronizedList(); 假如你创建的代码如下:List<Map<String,Object>& ...