首页 技术 正文
技术 2022年11月23日
0 收藏 892 点赞 3,396 浏览 4804 个字
main.xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> <ListView android:id="@+id/lv_single_list"
android:descendantFocusability="blocksDescendants"
android:fastScrollEnabled="true" android:clickable="true"
android:divider="@drawable/divider_horizontal_timeline"
android:dividerHeight="1.0dip" android:layout_width="fill_parent"
android:layout_height="400dp" android:scrollingCache="false"
android:fadingEdge="none" android:cacheColorHint="#00000000" /></LinearLayout>
适配器adapter的配置文件list_adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"
android:orientation="horizontal"> <TextView android:id="@+id/tv_zxing_section_sequence"
android:layout_width="50dp" android:layout_height="wrap_content"
android:textSize="16sp" /> <TextView android:id="@+id/tv_zxing_sectionname"
android:layout_width="210dp" android:layout_height="wrap_content"
android:textSize="14sp" android:layout_marginLeft="10dp" /> <CheckBox android:id="@+id/item_cb_section" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false"
android:focusableInTouchMode="false" android:clickable="false"
android:layout_alignParentTop="true" android:layout_marginLeft="5dp" /></LinearLayout>
SingleListChoiceActivity.javapackage com.amker.test;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;public class SingleListChoiceActivity extends Activity { private ListView listView; private Map<Integer, Boolean> isSelected; private List beSelectedData = new ArrayList(); ListAdapter adapter; private List cs = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) this.findViewById(R.id.lv_single_list);
cs = new ArrayList();
cs.add("aaaaaa");
cs.add("bbbbbb");
cs.add("cccccc");
cs.add("dddddd");
cs.add("eeeeee");
cs.add("ffffff");
cs.add("gggggg");
cs.add("hhhhhh");
cs.add("jjjjjj"); initList();
} void initList(){ if (cs == null || cs.size() == )
return;
if (isSelected != null)
isSelected = null;
isSelected = new HashMap<Integer, Boolean>();
for (int i = ; i < cs.size(); i++) {
isSelected.put(i, false);
}
// 清除已经选择的项
if (beSelectedData.size() > ) {
beSelectedData.clear();
}
adapter = new ListAdapter(this, cs);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("map", cs.get(position).toString());
}
}); } class ListAdapter extends BaseAdapter { private Context context; private List cs; private LayoutInflater inflater; public ListAdapter(Context context, List data) {
this.context = context;
this.cs = data;
initLayoutInflater();
} void initLayoutInflater() {
inflater = LayoutInflater.from(context);
} public int getCount() {
return cs.size();
} public Object getItem(int position) {
return cs.get(position);
} public long getItemId(int position) {
return ;
} public View getView(int position1, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View view = null;
final int position = position1;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_adapter,
null);
holder = new ViewHolder();
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.item_cb_section);
holder.tv_sequence = (TextView) convertView
.findViewById(R.id.tv_zxing_section_sequence);
holder.tv_sectionname = (TextView) convertView
.findViewById(R.id.tv_zxing_sectionname);
convertView.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
holder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 当前点击的CB
boolean cu = !isSelected.get(position);
// 先将所有的置为FALSE
for(Integer p : isSelected.keySet()) {
isSelected.put(p, false);
}
// 再将当前选择CB的实际状态
isSelected.put(position, cu);
ListAdapter.this.notifyDataSetChanged();
beSelectedData.clear();
if(cu) beSelectedData.add(cs.get(position));
}
});
holder.tv_sequence.setText(String.valueOf(position + ));
holder.tv_sectionname.setText(cs.get(position).toString());
holder.checkBox.setChecked(isSelected.get(position));
return convertView;
}
} class ViewHolder { CheckBox checkBox; TextView tv_sequence; TextView tv_sectionname; }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731