简单实现listview可选择的列表(不用重写adapter)

   接到新任务是完成向用户群发短信的功能,首先是将所有用户以listview展示出来。其次可以进行选择,并且全选。

首先listview的布局为

<?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="match_parent"
    android:orientation="horizontal"
    android:id="@+id/lll" >

    <TextView
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckedTextView
        android:id="@+id/data"
        android:layout_weight="1"
        android:layout_width="0dp"
         android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:layout_height="wrap_content"/>

</LinearLayout>

在activity中给item添加点击事件


listview.setOnItemClickListener(new OnItemClickListener() {


<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void onItemClick(AdapterView<?> parent, View view,
<span style="white-space:pre">					</span>int position, long id) {
<span style="white-space:pre">				</span>// TODO Auto-generated method stub
<span style="white-space:pre">				</span>ArrayList<Map<String, Object>> l=getdata();//获取用户列表
<span style="white-space:pre">				</span>numlist.add(l.get(position).get("num"));//根据点击的position,将要
<span style="white-space:pre">				</span>CheckedTextView  checktv = (CheckedTextView) parent.getChildAt(position).findViewById(R.id.data);
<span style="white-space:pre">				</span>if(checktv.isChecked()){   
<span style="white-space:pre">					</span>checktv.setChecked(false);
<span style="white-space:pre">					</span>numlist.remove(l.get(position).get("num"));
<span style="white-space:pre">					</span>//删除numlist的值


<span style="white-space:pre">				</span>}else{   
<span style="white-space:pre">					</span>checktv.setChecked(true);   
<span style="white-space:pre">					</span>numlist.add(l.get(position).get("num"));//根据点击的position,将被点击的用户加入发送列表
<span style="white-space:pre">				</span>}   
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>});

全选点击事件

同listitem点击事件,获取listview的每个孩子view。

btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				LayoutInflater layout=List.this.getLayoutInflater();
				View view=layout.inflate(R.layout.list_item, null);
				ListView lv=(ListView)findViewById(R.id.listview);
				if("全选"==btn1.getText()){
					btn1.setText("取消");
					String n=""+lv.getChildCount();
					Log.e("dasf", n);
					for(int i=0;i<lv.getChildCount();i++){
						CheckedTextView ct = (CheckedTextView) lv.getChildAt(i).findViewById(R.id.data);
						ct.setChecked(true);
					}
					numlist.addAll(getdata());
				}
				else{
					btn1.setText("全选");
					for(int i=0;i<lv.getChildCount();i++){

						CheckedTextView ct = (CheckedTextView) lv.getChildAt(i).findViewById(R.id.data);
						ct.setChecked(false);
					}
					numlist.clear();
				}
			}




猜你喜欢

转载自blog.csdn.net/mace_android/article/details/47082017