android - 如何从卡片视图中删除自定义卡片
问题描述
我正在使用自定义卡片视图来显示服务器数据。我有一堆一张一张出现的卡片。所以,我想知道我应该如何在点击删除按钮时删除整张卡片。扫描仪图像
这是适配器代码:
public class CardAdapter<M> extends BaseCardAdapter implements View.OnLongClickListener {
private List<Model> modelList;
private Context context;
TextView tvCount;
public CardAdapter(List<Model> modelList, Context context, TextView tvCount) {
this.modelList = modelList;
this.context = context;
this.tvCount = tvCount;
}
@Override
public int getCount() {
return modelList.size();
}
@Override
public int getCardLayoutId() {
return R.layout.card_item;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBindData(final int position, final View cardview) {
// if (modelList==null || modelList.size()==0){
// return;
// }
// tvCount.setText((position)+"/"+modelList.size());
// ImageView imageView=(ImageView)cardview.findViewById(R.id.imageView);
TextView textView = (TextView) cardview.findViewById(R.id.textView);
TextView textView1 = (TextView) cardview.findViewById(R.id.textView1);
TextView textView2 = (TextView) cardview.findViewById(R.id.textView2);
TextView textView3 = (TextView) cardview.findViewById(R.id.textView3);
TextView textView4 = (TextView) cardview.findViewById(R.id.textView4);
TextView textView5 = (TextView) cardview.findViewById(R.id.textView5);
TextView textView6 = (TextView) cardview.findViewById(R.id.textView6);
TextView textView7 = (TextView) cardview.findViewById(R.id.textView7);
final Model model = modelList.get(position);
//textView.setText ("Name" + ": " + model.getName());
String name = "<font color=#FFFFFF>Name:</font>";
String nameOne = "<font color=#FFFFFF>" + " " + model.getName() + " </font>";
textView.setText(Html.fromHtml(name + nameOne));
//textView1.setText("Job Title" + ": " + model.getJobTitle());
String job = "<font color=#FFFFFF>Job Title:</font>";
String jobOne = "<font color=#FFFFFF>" + " " + model.getJobTitle() + " </font>";
textView1.setText(Html.fromHtml(job + jobOne));
//textView2.setText("Company" + ": " + model.getCompany());
String company = "<font color=#FFFFFF>Company:</font>";
String companyOne = "<font color=#FFFFFF>" + " " + model.getCompany() + " </font>";
textView2.setText(Html.fromHtml(company + companyOne));
//textView3.setText("Telephone" + ": " + model.getTelephone());
String telephone = "<font color=#FFFFFF>Telephone:</font>";
String telephoneOne = "<font color=#FFFFFF>" + " " + model.getTelephone() + " </font>";
textView3.setText(Html.fromHtml(telephone + telephoneOne));
//textView4.setText("Mobile" + ": " + model.getMobile());
String mobile = "<font color=#FFFFFF>Mobile:</font>";
String mobileOne = "<font color=#FFFFFF>" + " " + model.getMobile() + " </font>";
textView4.setText(Html.fromHtml(mobile + mobileOne));
//textView5.setText("Email" + ": " + model.getEmail());
String email = "<font color=#FFFFFF>Email:</font>";
String emailOne = "<font color=#FFFFFF>" + " " + model.getEmail() + " </font>";
textView5.setText(Html.fromHtml(email + emailOne));
//textView6.setText("Address" + ": " + model.getAddress());
String address = "<font color=#FFFFFF>Address:</font>";
String addressOne = "<font color=#FFFFFF>" + " " + model.getAddress() + " </font>";
textView6.setText(Html.fromHtml(address + addressOne));
//textView7.setText("Narration" + ": " + model.getNarration());
String narration = "<font color=#FFFFFF>Narration:</font>";
String narrationOne = "<font color=#FFFFFF>" + " " + model.getNarration() + " </font>";
textView7.setText(Html.fromHtml(narration + narrationOne));
cardview.findViewById(R.id.textView).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Model model = modelList.get(position);
customDialogAddContactnShare(model);
return false;
}
});
cardview.findViewById(R.id.textView2).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Model model = modelList.get(position);
customDialogSearch(model.getCompany());
return false;
}
});
cardview.findViewById(R.id.textView4).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Model model = modelList.get(position);
customeDilogMobCallnMsg(model.getMobile());
return false;
}
});
cardview.findViewById(R.id.textView3).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Model model = modelList.get(position);
customDilogTeleCallnMsg(model.getTelephone());
return false;
}
});
cardview.findViewById(R.id.textView5).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Model model = modelList.get(position);
customDialogMail(model.getEmail());
return false;
}
});
cardview.findViewById(R.id.textView6).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Model model = modelList.get(position);
customDialogMapAddress(model.getAddress());
return false;
}
});
//tvCount.setText(modelList.size());
//Toast.makeText(context," " + modelList.size(),Toast.LENGTH_SHORT).show();
// Picasso.with(context).load(model.getImage()).into(imageView);
}
@Override
public boolean onLongClick(View v) {
return false;
}
像这样。在这里,我想删除第一个位置卡片(刷卡后出现的卡片)。如果用户单击删除按钮,则第一张卡将被删除。
帮我解决这个问题。
先感谢您!!
解决方案
为此,您可以先在适配器中找到位置,然后在该位置调用删除卡的方法,如下所示,从数据库中获取数据
private void deletePlace(int position) {
DBOpenHelper dbOpenHcelper = new DBOpenHelper(context);
dbOpenHelper.deleteData(arrayList.get(position).getEVENT());
arrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,arrayList.size());
}
我希望这对你有帮助!!