您的位置:首页 > 其它

Listview结合checkbox实现删除功能

2014-03-24 18:34 295 查看


效果图

实现要点:

1 Listview是一个动态的组件,实现监听Listview里的动作需要用到BaseAdapter类

2 数据存储结构ArrayList<HashMap<String, String>> list, 第一个string存放flag,用以判断是否选中;第二个string存放msg

3 一旦数据有变,必须使用mAdapter.notifyDataSetChanged(); 否则view无法得知数据变化,从而会报错

一共由以下文件组成(仅核心部分,参考慎用)

JAVA

Button btnDelete;
ListView lv;
Context mContext;
MyListAdapter adapter;
private ArrayList<HashMap<String, String>> list;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_carton_no_main);
SysApplication.getInstance().addActivity(this);

btnDelete = (Button)findViewById(R.id.ctn_no_scan_delete_btn);
lv = (ListView)findViewById(R.id.carton_no_list);

list = new ArrayList<HashMap<String, String>>();
mAdapter = new MyListAdapter(list, this);
lv.setAdapter(mAdapter);

//insert something
HashMap<String, String> map = new HashMap<String, String>();
map.put("content", 'test');
map.put("flag", "false");
list.add(map);

btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Iterator<HashMap<String, String>> iterator = list.iterator();
while (iterator.hasNext()) {
HashMap<String, String> temp = iterator.next();
if (temp.get("flag").equals("true")) {
iterator.remove();
}
}
checkNum = 0;
//if you have changed list, please excute mAdapter.notifyDataSetChanged();
dataChanged();
}
});

lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ViewHolder holder = (ViewHolder) arg1.getTag();
holder.cb.toggle();

//store checkbox's status
if (holder.cb.isChecked() == true) {
list.get(arg2).put("flag", "true");
checkNum++;
} else {
list.get(arg2).put("flag", "false");
checkNum--;
}
}
});
}
public class MyListAdapter extends BaseAdapter{

private ArrayList<HashMap<String, String>> list;

private Context context;

private LayoutInflater inflater = null;

public MyListAdapter(ArrayList<HashMap<String, String>> list, Context context){
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {

return list.size();
}
@Override
public Object getItem(int position) {

return list.get(position);
}
@Override
public long getItemId(int position) {

return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.activity_carton_no_list, null);
holder.tv = (TextView) convertView.findViewById(R.id.carton_no_list_name);
holder.cb = (CheckBox) convertView.findViewById(R.id.carton_no_list_checked);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//init view
holder.tv.setText(list.get(position).get("content").toString());
holder.cb.setChecked(list.get(position).get("flag").equals("true"));
return convertView;
}

final class ViewHolder{
TextView tv;
CheckBox cb;
}
}
private void dataChanged(){

mAdapter.notifyDataSetChanged();
}


XML

1/2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">

<CheckBox
android:id="@+id/carton_no_list_checked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical" />

<TextView
android:id="@+id/carton_no_list_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="Name"
android:textColor="@color/viewfinder_mask"
android:textSize="20dp" />

</LinearLayout>
</LinearLayout>


2/2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.1">
<ListView
android:id="@+id/carton_no_list"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:orientation="horizontal" >

<Button
android:id="@+id/ctn_no_scan_delete_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.1"
android:text="Delete" />

</LinearLayout>
</LinearLayout>


同理,实现radiobox,也是一样的道理。

One thing leads to another...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐