You do not need to use checkbox to select all Android ListView entries!
Hello everyone, I flipped through my previous notes and found a feature that I specifically marked, that is, the Select All feature of ListView. I suddenly remembered my hard work at that time, because of my college mechanical background, I did not know what code I was after my graduation. I did some sales at 58. I can't do it anymore. If I don't do it, I have to find a job online. I like computer-related jobs. One day, a training institution calls me and asks me to go for an interview, I thought that if I had to go to work, who would know that it was just a few days before I went to training? Of course I could not spend any money, so I went back directly. After thinking for a few days, I really didn't know what to do except for sales, bid your teeth and spend money on training. I will erase it. I will not tell you about it. I will write down this function. Although it is very simple, it was very difficult for me at the time, so I wrote it for some new Android users. Although I am still a good cook, I will try my best.
In fact, it is also very simple to create a set in the adapter, mainly containing the selected entries, write some add, clear methods. Put a check mark in the list_item file. It will be displayed when you click it for the first time, and click "hide" to loop it up. Let's take a look, as shown in:
First, let me look at the code in the main Activity, as shown below:
Package com. zqy. listviewdelete; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. button; import android. widget. listView; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private List
Strings = new ArrayList
(); Private ListView mListView; private ListAdapter adapter; private Button btnCheckall, btnCancel; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mListView = (ListView) findViewById (R. id. listView1); adapter = new ListAdapter (this); initVar (); initView (); adapter. setdata (strings); mListView. setAdapter (adapter); mListView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
Parent, View view, int position, long id) {/*** add it to the selected set and remove it. if not, add it to the Set */if (adapter. isItemSelected (position) {adapter. removeSelected (position);} else {adapter. addSelected (position);} adapter. notifyDataSetChanged () ;}}) ;}/ *** initialization control */private void initView () {btnCheckall = (Button) findViewById (R. id. btn_checkall); btnCancel = (Button) findViewById (R. id. btn_cancel); btnCheckall. setOnClickListener (this); btnCancel. setOnClickListener (this);}/*** add analog data */private void initVar () {for (int I = 0; I <20; I ++) {strings. add (I + "") ;}@overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_checkall:/*** first, remove the selected entry before all selections */adapter. removeselected ();/** traverse the set and add it to the selected set */for (int I = 0; I <strings. size (); I ++) {adapter. addSelected (I); adapter. notifyDataSetChanged ();} break; case R. id. btn_cancel:/*** clear selected set */adapter. removeselected (); adapter. yydatasetchanged (); break; default: break ;}}}
Very simple. initialize the control, add virtual data, add the selected set, select all traversal, and delete and clear the set.
Let's take a look at the code in the adapter:
Package com. zqy. listviewdelete; import java. util. arrayList; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView; public class ListAdapter extends BaseAdapter {private Context mContext; private List
List;/*** defines a selected set */private List
Seleteds; public ListAdapter (Context context) {super (); this. mContext = context; list = new ArrayList
(); Seleteds = new ArrayList
();} Public void setdata (List
Strings) {list. addAll (strings) ;}@ Overridepublic int getCount () {return list. size () ;}@ Overridepublic Object getItem (int position) {return list. get (position) ;}@ Overridepublic long getItemId (int position) {return position;}/*** add to the selected set * @ param position */public void addSelected (int position) {seleteds. add (position);} public void removeSelected (Integer position) {if (seleteds. contains (position) {sele Teds. remove (position) ;}/ *** clear selected set */public void removeselected () {seleteds. clear ();}/** determine whether the current entry is full */public boolean isItemSelected (int position) {return seleteds. contains (position )? True: false ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {convertView = LayoutInflater. from (mContext ). inflate (R. layout. list_item, null); holder = new ViewHolder (); holder. textView = (TextView) convertView. findViewById (R. id. textView1); holder. imageView = (ImageView) convertView. findViewById (R. id. imageView1); convertView. setTag (hol Der);} else {holder = (ViewHolder) convertView. getTag ();}/*** because convertView is reused, You need to initialize the selected image as needed. Avoid confusion */holder. imageView. setVisibility (View. INVISIBLE);/** display the selected image when the selected set contains entries */if (seleteds. contains (position) {holder. imageView. setVisibility (View. VISIBLE);} holder. textView. setText (list. get (position); return convertView;} private static class ViewHolder {TextView textView; ImageView imageView ;}}
There are several methods, such as adding methods and clearing methods.
Well, today is the end of the day. If you have any questions and leave a message, you can only write this in your diary. For Beginners, do not try it!
Click here to download the source code