The List. removeAll () method is invalid, and list. removeall is invalid.

Source: Internet
Author: User

The List. removeAll () method is invalid, and list. removeall is invalid.

The List. removeAll () method is invalid.

A few days ago, I encountered List. the removeAll () method fails and has not been tested for half a day. After debugging with the boss for half a day, I finally found out the cause. If anyone encounters this strange problem, I can refer to it for reference, it may be helpful to you. If you don't talk much about it, simply go to the code.

Display the home page of the data

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;
}

}

}

 

Entity used for testing

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 + "]";
}

}

 

 

Class for adding data to an object

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;

}

}

Main layout File

<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 Layout

<? 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 = "text 3"
Android: layout_height = "wrap_content"/>

</LinearLayout>

 

 

The cause of list. removeAll failure is that

// Value = new AddValue ();
// List1 = value. setValue ();

These two codes re-instantiate the original data and re-obtain the value of list1.

Although the new list1 value is the same as the original value, the system cannot accurately proofread list2, that is, the value in list2 cannot be removed normally (I guess it may be related to the memory, please give us a reasonable explanation)

I also tested it. If list1 is set to string, the list1 value is assigned again, and list. removeAll () is still valid.

 

 

 

Note: If the writing is not good, please criticize and point out the shortcomings. You are welcome to speak out.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.