标签:
List.removeAll()方法失效
前几天遇到List.removeAll()方法失效,测试了半天都没测出来,后面跟老大在那边调试了半天,最后终于找出原因,以后要是谁遇到这个奇葩的问题可以借鉴参考下,或许会对你有所帮助,废话不多说,直接上代码
显示数据的主页面
public class MainActivity extends Activity {
private ListView listview1;
private List<BPOS_HospitalUnInStockInfo> list1;
private List<BPOS_HospitalUnInStockInfo> list2;
private AddValue value;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview1=(ListView) findViewById(R.id.listview1);
value =new AddValue();
list1=value.setValue();
myAdapter=new MyAdapter(list1);
listview1.setAdapter(myAdapter);
listview1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
BPOS_HospitalUnInStockInfo info=(BPOS_HospitalUnInStockInfo) arg0.getItemAtPosition(arg2);
if(list2==null){
list2=new ArrayList<BPOS_HospitalUnInStockInfo>();
}
list2.add(info);
if(list2!=null){
//value =new AddValue();
//list1=value.setValue();
boolean b=list1.removeAll(list2);
myAdapter.setList(list1);
}
}
});
}
private class MyAdapter extends BaseAdapter{
List<BPOS_HospitalUnInStockInfo> list;
public MyAdapter(List<BPOS_HospitalUnInStockInfo> list) {
// TODO Auto-generated constructor stub
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void setList(List<BPOS_HospitalUnInStockInfo> list){
this.list=list;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder=null;
if(convertView==null){
viewHolder=new ViewHolder();
convertView=View.inflate(MainActivity.this, R.layout.myitem, null);
viewHolder.textView=(TextView) convertView.findViewById(R.id.textview3);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(list.get(position).getChengfenming());
final String str=viewHolder.textView.getText().toString();
return convertView;
}
class ViewHolder{
TextView textView;
}
}
}
用来测试的实体
public class BPOS_HospitalUnInStockInfo {
private String yiyuanzhujian;
private String id;
private String xueyechengfeng;
private String chengfenming;
private String rh;
private double ashuliang;
private double bshuliang;
private double oshuliang;
private double abshuliang;
private double adaishu;
public String getYiyuanzhujian() {
return yiyuanzhujian;
}
public void setYiyuanzhujian(String yiyuanzhujian) {
this.yiyuanzhujian = yiyuanzhujian;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getXueyechengfeng() {
return xueyechengfeng;
}
public void setXueyechengfeng(String xueyechengfeng) {
this.xueyechengfeng = xueyechengfeng;
}
public String getChengfenming() {
return chengfenming;
}
public void setChengfenming(String chengfenming) {
this.chengfenming = chengfenming;
}
public String getRh() {
return rh;
}
public void setRh(String rh) {
this.rh = rh;
}
public double getAshuliang() {
return ashuliang;
}
public void setAshuliang(double ashuliang) {
this.ashuliang = ashuliang;
}
public double getBshuliang() {
return bshuliang;
}
public void setBshuliang(double bshuliang) {
this.bshuliang = bshuliang;
}
public double getOshuliang() {
return oshuliang;
}
public void setOshuliang(double oshuliang) {
this.oshuliang = oshuliang;
}
public double getAbshuliang() {
return abshuliang;
}
public void setAbshuliang(double abshuliang) {
this.abshuliang = abshuliang;
}
public double getAdaishu() {
return adaishu;
}
public void setAdaishu(double adaishu) {
this.adaishu = adaishu;
}
@Override
public String toString() {
return "BPOS_HospitalUnInStockInfo [yiyuanzhujian=" + yiyuanzhujian
+ ", id=" + id + ", xueyechengfeng=" + xueyechengfeng
+ ", chengfenming=" + chengfenming + ", rh=" + rh
+ ", ashuliang=" + ashuliang + ", bshuliang=" + bshuliang
+ ", oshuliang=" + oshuliang + ", abshuliang=" + abshuliang
+ ", adaishu=" + adaishu + "]";
}
}
为实体添加数据的类
public class AddValue {
public List<BPOS_HospitalUnInStockInfo> setValue(){
List<BPOS_HospitalUnInStockInfo> list=new ArrayList<BPOS_HospitalUnInStockInfo>();
BPOS_HospitalUnInStockInfo info;
for(int i=0;i<10;i++){
info=new BPOS_HospitalUnInStockInfo();
info.setAbshuliang(i+1);
info.setAdaishu(i+1);
info.setAshuliang(i+1);
info.setBshuliang(i+1);
info.setChengfenming("Chengfenming"+(i+1));
info.setId("Id"+(i+1));
info.setOshuliang(i+1);
info.setRh("RH"+(i+1));
info.setXueyechengfeng("Xueyechengfeng"+(i+1));
info.setYiyuanzhujian("Yiyuanzhujian"+(i+1));
list.add(info);
}
return list;
}
}
主布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
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="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false" >
<TextView
android:id="@+id/textview3"
android:layout_margin="5dp"
android:layout_width="0dp"
android:layout_weight="1"
android:text="文本3"
android:layout_height="wrap_content"/>
</LinearLayout>
造成list.removeAll失败的原因就是因为
//value =new AddValue();
//list1=value.setValue();
这两句代码,把原先的数据重新实例化,重新获取list1的值。
虽然获取到新的list1和原来的值是一样的,但是系统已经无法准确校对list2了,也就是无法正常移除list2中的值(个人猜测可能是跟内存有关,请大神们支招,给出合理的解释吧)
个人还测试了一下,如果list1放的是string,重新给list1赋值,list.removeAll()仍然有效
后记:如果写的不好,不足之处请大家批评指出,欢迎大家吐槽
标签:
原文地址:http://www.cnblogs.com/linwh/p/5057980.html