单选。多选,删除

来源:互联网 发布:网络免费小说一号红人 编辑:程序博客网 时间:2024/06/07 20:05
package com.example.d924_monthexam;


import java.util.ArrayList;


import com.x.d924_monthexam.adapter.MyAdapter;
import com.x.d924_monthexam.bean.Exam;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;


public class MainActivity extends Activity implements OnClickListener {


private ListView lv;
private Button b1;
private Button b2;
private Button b3;
private ArrayList<Exam> al;
private  ArrayList<Exam> al1= new ArrayList<Exam>();
private MyAdapter ma;
private boolean isChecked=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
ma = new MyAdapter(this,al);
lv.setAdapter(ma);
}


private void initData() {
al = new ArrayList<Exam>();
al.add(new Exam("fucking month exam1",false));
al.add(new Exam("fucking month exam2",false));
al.add(new Exam("fucking month exam3",false));
al.add(new Exam("fucking month exam4",false));
al.add(new Exam("fucking month exam5",false));
al.add(new Exam("fucking month exam6",false));
al.add(new Exam("fucking month exam7",false));
al.add(new Exam("fucking month exam8",false));
al.add(new Exam("fucking month exam9",false));
al.add(new Exam("fucking month exam10",false));
al.add(new Exam("fucking month exam11",false));
al.add(new Exam("fucking month exam12",false));
}


private void initView() {
lv = (ListView) findViewById(R.id.lv);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
chose();
break;
case R.id.b2:
unChose();
break;
case R.id.b3:
delete();
break;


default:
break;
}
ma.notifyDataSetChanged();
}


private void chose() {
if (!isChecked) {
for (int i = 0; i < al.size(); i++) {
if (al.get(i).isChecked()==false) {
al.get(i).setChecked(true);
}
}
isChecked=true;
}else if(isChecked){
for (int i = 0; i < al.size(); i++) {
if (al.get(i).isChecked()==true) {
al.get(i).setChecked(false);
}
}
isChecked=false;
}

}
private void unChose() {
for (int i = 0; i < al.size(); i++) {
al.get(i).setChecked(!al.get(i).isChecked());
}
}


private void delete() {
for (int i = 0; i < al.size(); i++) {
if (al.get(i).isChecked()) {
al1.add(al.get(i));
}
}
al.removeAll(al1);
}
}
0 0
原创粉丝点击