怎么在项被删除时刷新RecyclerView?

huangapple go评论76阅读模式
英文:

How to refresh recyclerView when item deleted?

问题

Sure, here is the translation of your provided code:

KelimeleriGoster.class

package com.tcoding.kelimeezberletici;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.tcoding.kelimeezberletici.Adapter.KelimeleriGosterAdapter;
import com.tcoding.kelimeezberletici.KelimeEkle.KelimeEkle;
import com.tcoding.kelimeezberletici.Sınıflar.Kelimeler;
import com.tcoding.kelimeezberletici.Veritabani.Veritabani;
import com.tcoding.kelimeezberletici.Veritabani.VeritabaniDao;

import java.util.ArrayList;

public class KelimeleriGoster extends AppCompatActivity {
    private RecyclerView rv;
    private FloatingActionButton floatingActionButton;
    private ArrayList<Kelimeler> kelimelerArrayList;
    private KelimeleriGosterAdapter adapter;
    private Veritabani vt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kelimeleri_goster);

        rv = findViewById(R.id.rv);
        floatingActionButton = findViewById(R.id.floatingActionButton);
        vt = new Veritabani(this);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(this));

        kelimelerArrayList = new VeritabaniDao().kelimeleriGoster(vt);

        adapter = new KelimeleriGosterAdapter(this, kelimelerArrayList);
        rv.setAdapter(adapter);

        adapter.notifyDataSetChanged();

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(KelimeleriGoster.this, KelimeEkle.class));
                finish();
            }
        });
    }
}
KelimeleriGosterAdapter.class

package com.tcoding.kelimeezberletici.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tcoding.kelimeezberletici.R;
import com.tcoding.kelimeezberletici.Sınıflar.Kelimeler;
import com.tcoding.kelimeezberletici.Veritabani.Veritabani;
import com.tcoding.kelimeezberletici.Veritabani.VeritabaniDao;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class KelimeleriGosterAdapter extends RecyclerView.Adapter<KelimeleriGosterAdapter.KelimeViewHolder> {

    private Context context;
    private ArrayList<Kelimeler> kelimelerArrayList;
    private Veritabani vt;

    public KelimeleriGosterAdapter(Context context, ArrayList<Kelimeler> kelimelerArrayList) {
        this.context = context;
        this.kelimelerArrayList = kelimelerArrayList;
    }

    @NonNull
    @Override
    public KelimeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewdeneme, parent, false);
        return new KelimeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull KelimeViewHolder holder, int position) {

        final Kelimeler k = kelimelerArrayList.get(position);
        holder.textViewEng.setText(k.getIngilizce());
        holder.textViewTr.setText(k.getTurkce());
        holder.buttonSil.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vt = new Veritabani(context);
                String ingilizce = k.getIngilizce();
                String turkce = k.getTurkce();
                new VeritabaniDao().bildigimKelimelereEkle(vt, ingilizce, turkce);
                String id = String.valueOf(k.getId());
                new VeritabaniDao().sil(vt, id);
            }
        });
    }

    @Override
    public int getItemCount() {
        return kelimelerArrayList.size();
    }

    public class KelimeViewHolder extends RecyclerView.ViewHolder {
        private TextView textViewEng, textViewTr;
        private Button buttonSil;

        public KelimeViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewEng = itemView.findViewById(R.id.textViewEng);
            textViewTr = itemView.findViewById(R.id.textViewTr);
            buttonSil = itemView.findViewById(R.id.buttonSil);
        }
    }
}

Please note that I've only translated the code and removed any extraneous information. If you have any questions about the translated code or need further assistance, feel free to ask.

英文:

I have KelimeleriGoster.class. İts holding added word to database. There is recyclerview item in this class.

<a href="https://resimyukle.xyz/i/70eCH5"><img src="https://i.resimyukle.xyz/70eCH5.png" /></a>

i want to delete live when i click delete button. İ wanna refresh my list automatically

KelimeleriGoster.class
package com.tcoding.kelimeezberletici;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.tcoding.kelimeezberletici.Adapter.KelimeleriGosterAdapter;
import com.tcoding.kelimeezberletici.KelimeEkle.KelimeEkle;
import com.tcoding.kelimeezberletici.Sınıflar.Kelimeler;
import com.tcoding.kelimeezberletici.Veritabani.Veritabani;
import com.tcoding.kelimeezberletici.Veritabani.VeritabaniDao;
import java.util.ArrayList;
public class KelimeleriGoster extends AppCompatActivity {
private RecyclerView rv;
private FloatingActionButton floatingActionButton;
private ArrayList&lt;Kelimeler&gt; kelimelerArrayList;
private KelimeleriGosterAdapter adapter;
private Veritabani vt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kelimeleri_goster);
rv = findViewById(R.id.rv);
floatingActionButton = findViewById(R.id.floatingActionButton);
vt = new Veritabani(this);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
kelimelerArrayList = new VeritabaniDao().kelimeleriGoster(vt);
adapter = new KelimeleriGosterAdapter(this,kelimelerArrayList);
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(KelimeleriGoster.this,KelimeEkle.class));
finish();
}
});
}
}

And my Adapter. İts to show to data on recyclerview.. When i click delete button i wanna refresh my word list as live. Like liveData

KelimeleriGosterAdapter.class
package com.tcoding.kelimeezberletici.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.tcoding.kelimeezberletici.R;
import com.tcoding.kelimeezberletici.Sınıflar.Kelimeler;
import com.tcoding.kelimeezberletici.Veritabani.Veritabani;
import com.tcoding.kelimeezberletici.Veritabani.VeritabaniDao;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class KelimeleriGosterAdapter extends RecyclerView.Adapter&lt;KelimeleriGosterAdapter.KelimeViewHolder&gt; {
private Context context;
private ArrayList&lt;Kelimeler&gt; kelimelerArrayList;
private Veritabani vt ;
public KelimeleriGosterAdapter(Context context, ArrayList&lt;Kelimeler&gt; kelimelerArrayList) {
this.context = context;
this.kelimelerArrayList = kelimelerArrayList;
}
@NonNull
@Override
public KelimeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewdeneme,parent,false);
return new KelimeViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull KelimeViewHolder holder, int position) {
final Kelimeler k = kelimelerArrayList.get(position);
holder.textViewEng.setText(k.getIngilizce());
holder.textViewTr.setText(k.getTurkce());
holder.buttonSil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vt = new Veritabani(context);
//Sil dedigimizde ogrenilen kelimelere eklenecek o yuzden tanımlamalar yapılmalı
String ingilizce = k.getIngilizce();
String turkce = k.getTurkce();
new VeritabaniDao().bildigimKelimelereEkle(vt,ingilizce,turkce);//For the add another database
// Ekleme yapıldı idsi alınarak silme işlemi yapılmalı(Data Add)
String id = String.valueOf(k.getId());
//to delete data
new VeritabaniDao().sil(vt,id);
}
});
}
@Override
public int getItemCount() {
return kelimelerArrayList.size();
}
public class KelimeViewHolder extends RecyclerView.ViewHolder {
private TextView textViewEng,textViewTr;
private Button buttonSil;
public KelimeViewHolder(@NonNull View itemView) {
super(itemView);
textViewEng = itemView.findViewById(R.id.textViewEng);
textViewTr = itemView.findViewById(R.id.textViewTr);
buttonSil = itemView.findViewById(R.id.buttonSil);
}
}
}

How can i do it

答案1

得分: 1

你需要首先更新你的列表变量(kelimelerArrayList)。
然后使用 "notifyItemRemoved(position)"。
按照这个回答操作:
https://stackoverflow.com/a/38000604/3916792

英文:

You need to update ur list variable first ( kelimelerArrayList)
Then use "notifyItemRemoved(position)"
Follow this answer
https://stackoverflow.com/a/38000604/3916792

答案2

得分: 0

你可以使用DiffUtil类来实现这个功能。在你的适配器类中添加DiffUtil的调用将会处理在从列表中删除项目时的动画,并在删除项目时自动更新视图。以下是如何在适配器中使用DiffUtil类的示例:

class RunAdapter : RecyclerView.Adapter<RunAdapter.RunViewHolder>() {

    inner class RunViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    private val diffCallBack = object : DiffUtil.ItemCallback<Run>() {
        override fun areItemsTheSame(oldItem: Run, newItem: Run): Boolean {
            return oldItem.primaryKey == newItem.primaryKey
        }

        override fun areContentsTheSame(oldItem: Run, newItem: Run): Boolean {
            return oldItem.hashCode() == newItem.hashCode()
        }
    }

    private val differ = AsyncListDiffer(this, diffCallBack)

    // 使用这个函数将你的列表提交给适配器类
    fun submitList(listRun: List<Run>) = differ.submitList(listRun)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RunViewHolder {
        return RunViewHolder(LayoutInflater.from(parent.context).inflate(
            R.layout.item_run,
            parent,
            false
        ))
    }

    override fun getItemCount(): Int {
        return differ.currentList.size
    }

    override fun onBindViewHolder(holder: RunViewHolder, position: Int) {

        val run = differ.currentList[position]
        holder.itemView.apply {

            // 在这里设置你的视图
        }
    }
}

以下是一个Java示例,请根据你的需求进行修改:

public class RecyclerViewAdapter extends ListAdapter<EntityTableNotes, RecyclerViewHolder> {

    private RecyclerItemTouchListener mListener;

    public RecyclerViewAdapter() {
        super(diffCallback);
    }

    private static final DiffUtil.ItemCallback<EntityTableNotes> diffCallback = new DiffUtil.ItemCallback<EntityTableNotes>() {
        @Override
        public boolean areItemsTheSame(@NonNull EntityTableNotes oldItem, @NonNull EntityTableNotes newItem) {
            return oldItem.getNoteId() == newItem.getNoteId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull EntityTableNotes oldItem, @NonNull EntityTableNotes newItem) {
            return oldItem.getNoteTitle().equals(newItem.getNoteTitle()) &&
                    oldItem.getNoteBody().equals(newItem.getNoteBody()) &&
                    oldItem.getNotePriority() == newItem.getNotePriority();
        }
    };

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.singleitemgrid, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerViewHolder holder, final int position) {
        holder.noteTitle.setText(getItem(position).getNoteTitle());
    }
}

在活动或片段中,使用adapter.submitList(yourList)来提交数据给适配器。

英文:

You can use diffUtil class for that.Adding diffUtil calls in your adapter class will take care of the animations when you delete something from your list and update the view automatically when you delete an item. Here is an example of how to use diffUtils class in your adapter:

class RunAdapter: RecyclerView.Adapter&lt;RunAdapter.RunViewHolder&gt;()  {
inner class RunViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
private val diffCallBack = object : DiffUtil.ItemCallback&lt;Run&gt;(){
override fun areItemsTheSame(oldItem: Run, newItem: Run): Boolean {
return oldItem.primaryKey == newItem.primaryKey
}
override fun areContentsTheSame(oldItem: Run, newItem: Run): Boolean {
return oldItem.hashCode()==newItem.hashCode()
}
}
private val differ = AsyncListDiffer(this,diffCallBack)
//use this function for submitting your list to adapter class
fun submitList(listRun:List&lt;Run&gt;) = differ.submitList(listRun)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RunViewHolder {
return RunViewHolder(LayoutInflater.from(parent.context).inflate(
R.layout.item_run,
parent,
false
))
}
override fun getItemCount(): Int {
return differ.currentList.size
}
override fun onBindViewHolder(holder: RunViewHolder, position: Int) {
val run = differ.currentList[position]
holder.itemView.apply {
//set your view&#39;s here.
}
}
}

Here is an java example,modify it according to your needs:

public class RecyclerViewAdaptar extends ListAdapter&lt;EntityTableNotes, RecyclerViewHolder&gt; {
private RecyclerItemTouchListner mListner;
public RecyclerViewAdaptar() {
super(diffCallBack);
}
private static final DiffUtil.ItemCallback&lt;EntityTableNotes&gt; diffCallBack = new DiffUtil.ItemCallback&lt;EntityTableNotes&gt;() {
@Override
public boolean areItemsTheSame(@NonNull EntityTableNotes oldItem, @NonNull EntityTableNotes newItem) {
return oldItem.getNoteId() == newItem.getNoteId();
}
@Override
public boolean areContentsTheSame(@NonNull EntityTableNotes oldItem, @NonNull EntityTableNotes newItem) {
return oldItem.getNoteTitle().equals(newItem.getNoteTitle()) &amp;&amp;
oldItem.getNoteBody().equals(newItem.getNoteBody()) &amp;&amp;
oldItem.getNotePriority() == newItem.getNotePriority();
}
};
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new RecyclerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.singleitemgrid, parent, false));
}
}
@Override
public void onBindViewHolder(@NonNull final RecyclerViewHolder holder, final int position) {
holder.noteTitle.setText(getItem(position).getNoteTitle());
}

In activity/fragment use adapter.submitList(yourList) for submitting the data inside adapter.

huangapple
  • 本文由 发表于 2020年9月6日 23:05:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63765594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定