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.