CheckBox与ListView的结合方法一
程序员文章站
2022-07-16 22:38:29
...
最近这段时间比较闲,每天都无所事事。不再想重复这样的生活,于是申请了新的ITeye博客,给自己一个新的开始。今天是第一天写博客。
在Android下,ListView是使用较为频繁的一个组件,经常和很多其他组件结合完成很多功能。下面是本人使用的方法之一,以后会陆续编写其他的结合方法。可能有些人认为没必要,但是本人做过一段时间讲师,再有就是打发无聊的日子,所以有时间就倒腾倒腾。不再废话了,代码如下:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewOfCheCheckBoxActivity extends Activity {
private static final String TAG = ListViewOfCheCheckBoxActivity.class
.getName();
private ListView listView;
private String items[] = { "Java编程思想", "程序员修炼之道", "JUnit实践","UML实践","Mysql","Oracle","PowerDesigner","PowerBuilder","Eclipse","Netbeans", "极限编程之我行",
"Java网络编程", "Java并发编程", "Java算法", "性能分析","压力测试","软件工程","线性代数","微积分","离散数学","统计学","VM概述","操作系统原理","C语言","Spring","Hibernate","Spring MVC","Groovy","Grails","Struts2","Seam","JYpthon" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView messageTV = (TextView) findViewById(R.id.messageTV);
Button printSelectedCheckBoxButton = (Button) findViewById(R.id.printSelectedCheckBoxButton);
listView = (ListView) findViewById(R.id.simpleListViewOfCheckBox);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, items));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
printSelectedCheckBoxButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sb = new StringBuilder();
SparseBooleanArray selectedtems = listView.getCheckedItemPositions();
int selectItemCounts = selectedtems.size();
sb.append("节点总数:").append(items.length).append("、选中节点数:").append(selectItemCounts).append("。选中的节点有:");
for(int i=0;i<selectItemCounts;i++){
if(selectedtems.valueAt(i)){
int index = selectedtems.keyAt(i);
sb.append(index).append(" -> ").append(items[index]);
if(i<selectItemCounts -1){
sb.append("、");
}
String selectedItem =(String) listView.getAdapter().getItem(index);
Log.i(TAG, "选中的对象是:"+selectedItem);
}
}
selectedtems.clear();//需要清除以前的选中节点,否则选中节点数会不断累加下去,供下载的代码力没有这行代码,对不住了!
messageTV.setText(sb.toString());
}
});
}
}
布局较为简单,只是为了输出选中的值
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/messageTV" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="打印选中的复选框" android:id="@+id/printSelectedCheckBoxButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <ListView android:id="@+id/simpleListViewOfCheckBox" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView> </LinearLayout>
上一篇: ES6读后总结
下一篇: 转JSF注解seam