单击项目列表视图中的按钮时如何刷新列表

时间:2017-06-03 16:10:57

标签: android listview refresh

我在列表的所有项目上都有recyclerview和按钮。当我点击我的适配器项目中的按钮被删除,我想要刷新回收器并显示新数据。

@Override
  public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
    lietner = lietners.get(position);
    holder.txt_en.setText(lietner.getText_en());
    holder.txt_pe.setText(lietner.getText_pe());
    holder.time.setText(lietner.getTime());
    holder.data.setText(HelperCalendar.g2j(lietner.getTime_create()));
    holder.next_level.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      G.databaseOpenHelper.goToNextLevel(lietners.get(position).getId());

      }
    });
  }

2 个答案:

答案 0 :(得分:2)

重新初始化回收站视图并在 onBindViewHolder上获取回调

简单调用 notifyDataSetChanged();

       @Override
    public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
        lietner = lietners.get(position);

        holder.next_level.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // delete the item, or change the data.
                notifyDataSetChanged();
            }
        });
    }

答案 1 :(得分:0)

我这样做,它对我有用!

@Override
  public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
    lietner = lietners.get(position);
    holder.txt_en.setText(lietner.getText_en());
    holder.txt_pe.setText(lietner.getText_pe());
    holder.time.setText(lietner.getTime());
    holder.data.setText(HelperCalendar.g2j(lietner.getTime_create()));
    holder.next_level.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        G.databaseOpenHelper.goToNextLevel(lietners.get(position).getId());
        lietners.remove(position);
        notifyDataSetChanged();
      }
    });
  }