onClick()删除列表项并刷新

时间:2018-07-28 09:56:34

标签: android listview onclick

我有一个ListView,,其中每个项目都是可单击的。但是在每个项目中,我都有另一个可点击的按钮-应该会删除当前的项目onclick。

问题是,该列表是根据我的Visited.java类中的 JSON 生成的。

我尝试过:

在我的适配器中管理按钮单击。

@NonNull
@Override
public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
    View v = convertView;
    final ViewHolder holder;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.tvNazov = v.findViewById(R.id.tvNazov);
        holder.thumb = v.findViewById(R.id.ivImage);
        holder.tvTyp = v.findViewById(R.id.tvTyp);
        holder.tvPlace = v.findViewById(R.id.tvPlace);
        holder.tvPerex = v.findViewById(R.id.tvPerex);
        holder.tvLetter = v.findViewById(R.id.item_row_letter);
        holder.img1 = v.findViewById(R.id.imag1);
        holder.visiticon = v.findViewById(R.id.visiticon);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

   holder.thumb.setImageResource(R.drawable.ic_launcher_foreground);
   holder.tvNazov.setText(actorList.get(position).getNazov());

    final DatabaseHelper myDb;
    myDb = new DatabaseHelper(context);

    if(myDb.CheckDB(actorList.get(position).getExterier())) {
        holder.visiticon.setImageResource(R.drawable.visited2);
    } else {
        holder.visiticon.setImageResource(R.drawable.notvisited);
    }

    final View finalV = v;
    holder.visiticon
            .setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {
                    MediaPlayer mp = MediaPlayer.create(context,R.raw.click);
                    mp.start();
                   // holder.visiticon.playSoundEffect(SoundEffectConstants.CLICK);

                    myDb.ToggleVisit(actorList.get(position).getExterier());
                    if(myDb.CheckDB(actorList.get(position).getExterier())) {
                        holder.visiticon.setImageResource(R.drawable.visited2);
                    } else {
                        holder.visiticon.setImageResource(R.drawable.notvisited);
                        finalV.setVisibility(View.GONE);
                        notifyDataSetChanged();
                    }


                }
            });

   String pismeno = actorList.get(position).getLetter();

    if(pismeno.equalsIgnoreCase("x")) {holder.tvLetter.setVisibility(View.GONE);} else {holder.tvLetter.setVisibility(View.VISIBLE);holder.tvLetter.setText(pismeno);}

    Picasso.with(context).load(actorList.get(position).getThumb()).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(holder.thumb);
    holder.tvTyp.setText(actorList.get(position).getTyp());
    holder.tvPlace.setText(actorList.get(position).getPlace());
    holder.tvPerex.setText(actorList.get(position).getPerex());

    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(view.getContext(), SingleitemView.class);

            intent.putExtra("name", actorList.get(position).getNazov());
            intent.putExtra("adresar", actorList.get(position).getExterier());

            view.getContext().startActivity(intent);
        }
    });





    return v;
}

现在,当前项目是隐藏的onclick-正常运行,但是,那里仍然有空白,我需要刷新整个列表。

在适配器中使用notifyDataSetChanged();只会刷新列表,但是空白项仍然存在。因为我想我需要再次调用Visited.java类,或者不确定如何删除整个项目,不仅是列表中的内容。

1 个答案:

答案 0 :(得分:1)

请尝试添加点击侦听器以删除以下项目。

//Your click listener to delete item and refresh
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // If you want to delete item from DB then you need to call delete query here
                actorList.remove(position);
                notifyDataSetChanged();
            }
        });