gpt4 book ai didi

java - RecyclerView 设置最后一个ArrayList项数据为第一项

转载 作者:行者123 更新时间:2023-12-01 11:10:18 25 4
gpt4 key购买 nike

我有一个 RecyclerView 和一个具有 Person 模型的 ArrayList 的适配器。
当应用程序启动时,我在 EditText 中输入名称并按下按钮:

1) enter image description here

2) enter image description here

3) enter image description here

4) enter image description here

但是当我在 Activity 中记录 personList 数据时,结果是:

personList: Item 0 -----> Alireza
personList: Item 1 -----> Tohid
personList: Item 2 -----> Alireza

第一项被删除,最后一项在第一项中重复。 (我的适配器比较复杂,但我已经减少了代码和 View 以专注于主要问题)

适配器布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />

</LinearLayout>

适配器类:
public class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private ArrayList<Person> mList;
private View.OnClickListener listener;

public TestAdapter(ArrayList<Person> mList, View.OnClickListener listener) {
this.mList = mList;
this.listener = listener;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_adapter_item, parent, false);

if (viewType == Person.State.FIRST.getValue()) {
return new FirstViewHolder(itemView);
} else { // if (viewType == Person.State.SECOND.getValue())
return new SecondViewHolder(itemView);
}
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

final int pos = holder.getAdapterPosition();
final Person person = mList.get(pos);

if (holder instanceof FirstViewHolder) {

final FirstViewHolder h = (FirstViewHolder) holder;
h.tvName.setVisibility(View.GONE);
h.etName.setVisibility(View.VISIBLE);
h.etName.setText("");
h.etName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
person.setFirstName(s.toString());
}

@Override
public void afterTextChanged(Editable s) {

}
});

h.btnAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
person.setState(Person.State.SECOND);
notifyItemChanged(pos);

listener.onClick(v);
}
});

} else { // if (holder instanceof SecondViewHolder)

final SecondViewHolder h = (SecondViewHolder) holder;
h.tvName.setVisibility(View.VISIBLE);
h.tvName.setText(person.getFirstName());
h.etName.setVisibility(View.GONE);
h.btnAccept.setVisibility(View.GONE);
}
}

private static class FirstViewHolder extends RecyclerView.ViewHolder {

private TextView tvName;
private EditText etName;
private Button btnAccept;

private FirstViewHolder(@NonNull View itemView) {
super(itemView);

tvName = itemView.findViewById(R.id.tv_name);
etName = itemView.findViewById(R.id.et_name);
btnAccept = itemView.findViewById(R.id.button);
}
}

private static class SecondViewHolder extends RecyclerView.ViewHolder {

private TextView tvName;
private EditText etName;
private Button btnAccept;

SecondViewHolder(@NonNull View itemView) {
super(itemView);

tvName = itemView.findViewById(R.id.tv_name);
etName = itemView.findViewById(R.id.et_name);
btnAccept = itemView.findViewById(R.id.button);
}
}

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

@Override
public int getItemViewType(int position) {
return mList.get(position).getState().getValue();
}
}

Activity 相关部分:
ArrayList<Person> personList = new ArrayList<>();
personList.add(new Person("", Person.State.FIRST));
adapter = new TestAdapter(personList, new View.OnClickListener() {
@Override
public void onClick(View v) {
if (personList.size() < 3) {
personList.add(new Person("", Person.State.FIRST));
adapter.notifyItemInserted(personList.size());
} else {
for (int i = 0; i < personList.size(); i++) {
Log.i("personList", "Item " + i + " -----> " + personList.get(i).getName());
}
}
}
});

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

有什么问题?我怎样才能获得正确和真实的 ArrayList 数据?

最佳答案

您的问题在于 addTextChangedListener,当在回收站 View 中添加新项目时,旧的 TextChangedListeners 没有被删除。他们不断更改您的列表项值。当状态更改为 SECOND 状态时,您应该删除这些监听器(看看这个 How to remove all listeners added with addTextChangedListener 非常棘手)。或者您应该在触发 onTextChanged 时添加此验证。 if (person.getState() == Person.State.FIRST)
person.setFirstName(s.toString());

关于java - RecyclerView 设置最后一个ArrayList项数据为第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60828476/

25 4 0