編輯:關於android開發
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()仍然有效
後記:如果寫的不好,不足之處請大家批評指出,歡迎大家吐槽
本講的內容,理解起來很難,也許你看了很多資料也看不明白,但是用起來缺簡單的要命
Android開發:優化ListView實踐解析 在看了一些vogella的文章之後,發現關於android listview性能優化這一段很有意思,於是實踐了
Android基礎入門教程——9.3 使用Camera拍照 Android基礎入門教程——9.3 使用Camera拍照 標簽(空格分隔): A
Linux0.11內核--加載可執行二進制文件之1.copy_strings,notepad二進制copy從現在開始就是分析最後的核心模塊exec.c了,分析完這個文件後