大家好,又见面了,我是你们的朋友全栈君。
实现效果
主要使用方法 是, remove 之后在刷新界面
adapter 里面的代码
public class MyAdapter extends BaseAdapter {
private List<ListBean> mDatas = null;
private Context mContext = null;
public MyAdapter(Context context, List<ListBean> datas) {
mDatas = datas;
mContext = context;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_base_item, null);
holder.image = convertView.findViewById(R.id.image);
holder.title = convertView.findViewById(R.id.title);
holder.content = convertView.findViewById(R.id.content);
holder.delete = convertView.findViewById(R.id.btn);
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDatas.remove(position);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageResource(mDatas.get(position).getImgId());
holder.title.setText(mDatas.get(position).getTitle());
holder.content.setText(mDatas.get(position).getContent());
holder.delete.setText(mDatas.get(position).getDelete());
return convertView;
}
public final class ViewHolder {
ImageView image;
TextView title;
TextView content;
Button delete;
}
}
activity 里面的代码
public class ListBaseAdapterActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_adapter_activity_layout);
final List<ListBean> list = new ArrayList<>();
ListBean listBean = new ListBean(R.mipmap.one, "标题头", "内容内容", "删除");
list.add(listBean);
ListBean listBean1 = new ListBean(R.mipmap.ic_launcher, "今天", "内容内容", "删除");
list.add(listBean1);
ListBean listBean2 = new ListBean(R.mipmap.image, "明天", "内容内容", "删除");
list.add(listBean2);
ListBean listBean3 = new ListBean(R.mipmap.photo, "后天", "内容内容", "删除");
list.add(listBean3);
final ListView listView = findViewById(R.id.list);
final MyAdapter adapter = new MyAdapter(ListBaseAdapterActivity.this, list);
// 设置分割线
listView.setDivider(new ColorDrawable(Color.RED));
// 设置分割线的宽度
listView.setDividerHeight(5);
listView.setAdapter(adapter);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/160603.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...