This article describes the multiple and all functions of Android implementing the ListView control. Share it for your reference, as follows:
Main program code
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private ListView listView; private MyAdapter adapter; private ArrayList<String> items; //Simulate the collection of stored information private ArrayList<String> checked; //This collection stores the string displayed in the TextView in the selected list item private boolean isMultiple = false; // Record whether it is a multi-select state, true is yes, false is not @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); listView = (ListView) findViewById(); items = new ArrayList<String>(); ("00000"); ("11111"); ("22222"); ("33333"); ("44444"); ("55555"); ("66666"); ("77777"); ("88888"); ("99999"); ("aaaaa"); ("bbbbb"); ("ccccc"); ("ddddd"); adapter = new MyAdapter(items,this); //New to create a custom MyAdapter object (adapter); (new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { if(isMultiple){ CheckBox checkBox = (CheckBox) (); TextView textView = (TextView) (); if(()){ (false); (position, false); (); (position, ); int index = (()+""); (index); }else{ (true); (position, true); (); (position, ); (()+""); } } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { SubMenu subMenu = ("operate"); (0, 1, 0, "Multiple choice"); (0, 2, 0, "delete"); (0, 3, 0, "Select All"); (0, 4, 0, "Cancel all"); return (menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(()==1){ isMultiple = true; checked = new ArrayList<String>(); int index = (); for(int i=0;i<index;i++){ (i, ); } (); }else if(()==2){ for(String text : checked){ int index = (text); (index); } isMultiple = false; adapter = new MyAdapter(items,); (adapter); }else if(()==3){ isMultiple = true; checked = new ArrayList<String>(); int index = (); for(int i=0;i<index;i++){ (i, true); (i, ); (i, ); View view1 = (i, null, null); TextView textView = (TextView) (); (); /*CheckBox checkBox = (CheckBox) (); ();*/ (); (()+""); } }else if(()==4){ isMultiple = false; checked = null; int index = (); for(int i=0;i<index;i++){ (i, false); (i, ); (i, ); /*View view1 = (i, null, null); TextView textView = (TextView) (); (); CheckBox checkBox = (CheckBox) (); ();*/ (); } } return (item); } }
MyAdapter inherited from BaseAdapter
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MyAdapter extends BaseAdapter { private LayoutInflater inflater; private ArrayList<String> items; private Bitmap icon; private Context context; public Map<Integer,Boolean> checkedMap; //Save the status of whether checkbox is selected public Map<Integer,Integer> colorMap; //Save the status of textview Chinese text public Map<Integer,Integer> visibleMap; //Save the status of checkbox whether it displays public MyAdapter(ArrayList<String> items, Context context) { super(); = items; = context; inflater = (context); icon = ((), ); checkedMap = new HashMap<Integer, Boolean>(); colorMap = new HashMap<Integer, Integer>(); visibleMap = new HashMap<Integer, Integer>(); for(int i=0;i<();i++){ (i, false); (i, ); (i, ); } } @Override public int getCount() { // TODO Auto-generated method stub return (); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View view, ViewGroup arg2) { view = (.file_row, null); ImageView image = (ImageView) (); TextView text = (TextView) (); CheckBox checkBox = (CheckBox) (); ((position)); ((position)); (icon); ((position)); ((position)); return view; } }
Layout file for main interface
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android: android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Layout files used in MyAdapter
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" > <ImageView android: android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center_vertical" /> <TextView android: android:layout_width="80dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingLeft="5dp" /> <CheckBox android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:clickable="false" android:focusableInTouchMode="false" /> </LinearLayout>
Let’s analyze why we need to use map to save the status of checkbox and textview.
This is related to the refresh mechanism of ListView. When you have a lot of listview objects, every time you drag the listview up and down, the listview will be refreshed once. How to refresh? For example, on a screen, it only displays seven listviews at most. If you have ten data, when you want to see the eighth item, the first data will naturally be hidden, and the eighth item will be displayed. At this time, the listview will be refreshed. If you do not save the status of the checkbox you selected, if you choose the first checkbox status and the status of the checkbox is true, when you display the remaining eighth, ninth, and tenth data, the status of the checkbox status of the tenth checkbox will be displayed as true, but its status is not saved, it is just that you see that it has been selected, but in fact, you still choose the first data. This question is fucking. There is also a more bizarre state. If you make the checkbox's status true, the data must be greater than ten. If you keep dragging the screen up and down, you will see that the display status of the checkbox will jump randomly, but you actually choose the first piece of data, which will only make your users feel very unhappy.
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android layout layout tips summary》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android operating json format data skills》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.