ListView uses the CHOICE_MODE_MULTIPLE attribute to achieve multi-choice and listview multiple-choice

Source: Internet
Author: User

ListView uses the CHOICE_MODE_MULTIPLE attribute to achieve multi-choice and listview multiple-choice
:

Adapter:
public class CrazyAdapter extends BaseAdapter {    private Context mContext;    private List<String> mList;    public CrazyAdapter(Context mContext, List<String> mList) {        this.mContext = mContext;        this.mList = mList;    }    @Override    public int getCount() {        return mList.size();    }    @Override    public Object getItem(int position) {        return null;    }    @Override    public long getItemId(int position) {        return 0;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder = null;        if (convertView == null) {            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);            holder = new ViewHolder();            holder.view = (TextView) convertView.findViewById(R.id.tv_city);            holder.box = (CheckBox) convertView.findViewById(R.id.rb_view);            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        holder.view.setText(mList.get(position));        holder.box.setChecked(((ListView) parent).isItemChecked(position));        return convertView;    }    public final class ViewHolder {        private TextView view;        private CheckBox box;    }}
Page code:
Public class ListViewActivity extends Activity {private ListView mListView; private List <String> mList; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_list); mListView = (ListView) findViewById (R. id. ls_view); String [] mStrs = {" Chuan longzhijie", "San shimayu", "Chuan Duan Kang Cheng", "cun Shang chunshu", "dongye guiwu", "Zhang Ailing", "Jin Yong ", "Qian Zhongshu", "Lao She", "Liang Shiqiu", "Henry Miller", "Hemingway ", "Fiz", "Kerouac", "Jack London", "Tom", "Duras", "forloubay", "Hugo", "Balzac", "Shakespeare ", "Lawrence", "Mao", "Conan Doyle", "Flute"}; mList = Arrays. asList (mStrs); final CrazyAdapter mAdapter = new CrazyAdapter (this, mList); mListView. setAdapter (mAdapter); mListView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE); mListView. setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {mAdapter. policydatasetchanged ();}});}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.