全选?反选

时间:2016-08-13 15:12:33
【文件属性】:

文件名称:全选?反选

文件大小:2KB

文件格式:TXT

更新时间:2016-08-13 15:12:33

全选

package com.example.listviewdemo03; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.support.v4.app.NavUtils; public class MainActivity extends Activity implements OnClickListener { ListView lv; List str; Button b_all, b_del; CheckBox box; MyAdapter adapter; List pos=new ArrayList(); int position2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); str = new ArrayList(); for (int i = 0; i < 50; i++) { str.add("我是第" + i + "个~"); System.out.println(str.size()); } lv = (ListView) this.findViewById(R.id.lv); adapter=new MyAdapter(); lv.setAdapter(adapter); b_all = (Button) this.findViewById(R.id.b_all);//布局对应id b_del = (Button) this.findViewById(R.id.b_del); b_all.setOnClickListener(this); b_del.setOnClickListener(this); } class MyAdapter extends BaseAdapter { // 要显示的行数 public int getCount() { // TODO Auto-generated method stub return str.size(); } // 获取每一行的元素 public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } // 获取每一行的id public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } // 设置布局和内容 public View getView(int position, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub MainActivity.this.position2=position; // 用来把一个布局导入到java文件中,起到填充作用 View view = View.inflate(MainActivity.this, R.layout.activity_list, null); ImageView imageView = (ImageView) view.findViewById(R.id.iv); imageView.setImageResource(R.drawable.ic_launcher); TextView textView = (TextView) view.findViewById(R.id.tv); textView.setText(str.get(position));//点击第几条的view box = (CheckBox) view.findViewById(R.id.cb); box.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(box.isChecked()){ pos.add(position2); }else { pos.remove(position2); } } }); return view; } } // button点击事件 public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.b_all: box.setChecked(true); adapter.notifyDataSetChanged(); break; case R.id.b_del: for (int i = 0; i < pos.size(); i++) { str.remove(pos.get(i)); } adapter.notifyDataSetChanged(); break; } } }


网友评论