效果圖
實現要點:
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...