gpt4 book ai didi

java - 从 RecyclerView.Adapter 中删除项目会导致 OutOfBounds 异常

转载 作者:行者123 更新时间:2023-12-02 02:00:38 28 4
gpt4 key购买 nike

我正在尝试使用 RecyclerView 构建一个简单的列表,这将允许我添加/删除项目。

我可以删除这些项目,但在删除项目后,似乎没有“重新计算”位置。

例如,我的列表中有 20 个项目,如果删除最后一个项目,则位置为 19(理应如此)。该项目从列表中删除,但是当我再次单击删除最后一个项目时,位置仍然是 19,它应该是 18:

D/RecycleViewTest: Remove at Position: 19
D/RecycleViewTest: Remove at Position: 19

这会导致以下异常:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.recycleviewtest, PID: 1763
java.lang.IndexOutOfBoundsException: Index: 19, Size: 19
at java.util.ArrayList.remove(ArrayList.java:477)
at com.example.recycleviewtest.MainActivity$1.onRemoveClick(MainActivity.java:45)
at com.example.recycleviewtest.SimpleViewHolder$2.onClick(SimpleViewHolder.java:42)
at android.view.View.performClick(View.java:6205)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

我通过以下方式删除这些项目:

itemList.remove( position );
adapter.notifyItemRemoved( position );

我是否需要对某种函数进行额外的调用,以便“重新计算”列表?

这是主要 Activity :

public class MainActivity extends AppCompatActivity
{
List<SimpleViewModel> itemList;

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

itemList = generateSimpleList();

final SimpleAdapter adapter = new SimpleAdapter(itemList);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.simple_recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator( new DefaultItemAnimator() );
recyclerView.setAdapter(adapter);

adapter.setOnItemClickListener(new SimpleAdapter.onItemClickListener() {
@Override
public void onItemClick(int position)
{
Log.d( "RecycleViewTest", "Click at Position: " + position );
adapter.notifyItemChanged( position );
}

@Override
public void onRemoveClick( int position )
{
Log.d( "RecycleViewTest", "Remove at Position: " + position );
itemList.remove( position );
adapter.notifyItemRemoved( position );
}
});
}

private List<SimpleViewModel> generateSimpleList()
{
List<SimpleViewModel> simpleViewModelList = new ArrayList<>();

for (int i = 0; i < 20; i++) {
simpleViewModelList.add(new SimpleViewModel(String.format(Locale.US, "This is item %d", i)));
}

return simpleViewModelList;
}
}

这是我的 RecyclerView.Adapter(SimpleAdapter):

public class SimpleAdapter extends RecyclerView.Adapter {

private List<SimpleViewModel> models = new ArrayList<>();
private onItemClickListener mListener;

public SimpleAdapter(final List<SimpleViewModel> viewModels)
{
if (viewModels != null) {
this.models.addAll(viewModels);
}
}

public void setOnItemClickListener( onItemClickListener listener )
{
mListener = listener;
}

public interface onItemClickListener
{
void onItemClick( int position );
void onRemoveClick( int position);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new SimpleViewHolder(view, mListener);
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
((SimpleViewHolder) holder).bindData(models.get(position));
}

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

@Override
public int getItemViewType(final int position) {
return R.layout.item;
}
}

这是我的 RecyclerView.ViewHolder (SimpleViewHolder):

public class SimpleViewHolder extends RecyclerView.ViewHolder {

private TextView simpleTextView;
private ImageView imgRemove;

public SimpleViewHolder(final View itemView, final SimpleAdapter.onItemClickListener listener) {
super(itemView);
simpleTextView = (TextView) itemView.findViewById(R.id.simple_text);
imgRemove = (ImageView) itemView.findViewById(R.id.imgRemove);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( listener != null )
{
int position = getAdapterPosition();

if( position != RecyclerView.NO_POSITION )
{
listener.onItemClick( position );
}
}
}
});

imgRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( listener != null )
{
int position = getAdapterPosition();

if( position != RecyclerView.NO_POSITION )
{
listener.onRemoveClick( position );
}
}
}
});
}

public void bindData(final SimpleViewModel viewModel) {
simpleTextView.setText(viewModel.getSimpleText() );
}
}

请告诉我我哪里出了问题?谢谢!

更新:

我刚刚意识到,即使“播放”了删除动画,也没有实际的项目被删除,它们只是“重新出现”..

最佳答案

问题出在适配器的构造函数中:

private List<SimpleViewModel> models = new ArrayList<>();
private onItemClickListener mListener;

public SimpleAdapter(final List<SimpleViewModel> viewModels)
{
if (viewModels != null) {
this.models.addAll(viewModels);
}
}

适配器不会保留您传递给此构造函数的列表。相反,列表的内容会被复制到适配器自己的单独且不同的列表中。

这意味着稍后,当您执行时

@Override
public void onRemoveClick( int position )
{
Log.d( "RecycleViewTest", "Remove at Position: " + position );
itemList.remove( position );
adapter.notifyItemRemoved( position );
}

该项目已从 itemList 中删除,但从适配器的列表中删除。

您必须从适配器的复制列表中删除该项目,或者必须更改适配器以使其与 Activity 共享相同的列表。也许最简单的事情就是更改适配器:

private List<SimpleViewModel> models = new ArrayList<>();
private onItemClickListener mListener;

public SimpleAdapter(final List<SimpleViewModel> viewModels)
{
if (viewModels != null) {
this.models = viewModels; // instead of copying the contents
}
}

关于java - 从 RecyclerView.Adapter 中删除项目会导致 OutOfBounds 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51638345/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com