当点击删除按钮时,该列表项被删除,并且更新后的列表实时显示。
public class DeleteCityActivity extends AppCompatActivity implements View.OnClickListener{ ImageView errorIv,rightIv; ListView deleteLv; String[] hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州", "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"}; List<String> mDatas=new ArrayList<>();//列表数据源 List<String>deleteCitys=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_delete_city); deleteLv = findViewById(R.id.delete_lv);//找到组件 errorIv=findViewById(R.id.delete_iv_error); rightIv=findViewById(R.id.delete_iv_right); //传入数据 mDatas = new ArrayList<>(); for(String s:hotCitys){ mDatas.add(s); } //设置监听事件 errorIv.setOnClickListener(this); rightIv.setOnClickListener(this); //设置适配器 DeleteCityAdapter baseAdapter = new DeleteCityAdapter(this, mDatas, deleteCitys); deleteLv.setAdapter(baseAdapter); deleteLv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) { TextView deletecity=view.findViewById(R.id.item_delete_tv);//找到点击的textview String cityname=deletecity.getText().toString().trim();//城市名 System.out.println(pos+cityname);//找到位置 mDatas.remove(cityname); deleteCitys.add(cityname); baseAdapter.notifyDataSetChanged(); for(String tt:deleteCitys){ System.out.println("删除了"+tt+"\n"); } } }); } public void cancel(View view){ Toast.makeText(this,"跳转到城市管理页面",Toast.LENGTH_LONG).show(); } public void ok(View view){ String tmp=""; for(String s:deleteCitys) tmp=tmp+s+" "; Toast.makeText(this,"跳转到城市管理页面\n"+"删除了"+tmp,Toast.LENGTH_LONG).show(); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.delete_iv_error: cancel(view); break; case R.id.delete_iv_right: ok(view); break; } } }