gpt4 book ai didi

java - 为什么用相同的数据填充 RecyclerView 行?

转载 作者:行者123 更新时间:2023-11-29 04:07:52 25 4
gpt4 key购买 nike

我正在尝试使用存储在 ArrayList 中的数据来填充 RecyclerView。当 RecyclerView 加载每条数据时,每行重复 4 次。

我尝试了各种解决方案,但似乎都无法解决问题。

在对“mData”中的数据进行一些调试之后,似乎是正确的,所以这会让我相信这个问题是“onBindViewHolder”?

适配器

public class EventsRecyclerViewAdapter extends RecyclerView.Adapter<EventsRecyclerViewAdapter.ViewHolder> {

private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// data is passed into the constructor
public EventsRecyclerViewAdapter(List<String> data) {
this.mInflater = LayoutInflater.from(MainActivity.mainActivity);
this.mData = data;
}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {

holder.keyID.setText(mData.get(position));
holder.lockID.setText(mData.get(position));
holder.eventTime.setText(mData.get(position));
holder.eventType.setText(mData.get(position));
}

// total number of rows
@Override
public int getItemCount() {
return mData.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView keyID;
TextView lockID;
TextView eventTime;
TextView eventType;

ViewHolder(View itemView) {
super(itemView);
keyID = itemView.findViewById(R.id.keyIDTV);
lockID = itemView.findViewById(R.id.lockIDTV);
eventTime = itemView.findViewById(R.id.eventDateTV);
eventType = itemView.findViewById(R.id.eventTypeTV);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}




// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}

fragment

 public class KeyEvents extends Fragment {


public static KeyInfo newInstance() {
KeyInfo fragment = new KeyInfo();
return fragment;
}

EventsRecyclerViewAdapter adapter;

ArrayList<String> eventsList;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventsOperationHandler ev = new EventsOperationHandler();

eventsList = new ArrayList<>();

for (int i=0; i<ev.getEvents().size(); i++) {

eventsList.add(ev.getEvents().get(i).get(0));
eventsList.add(ev.getEvents().get(i).get(1));
eventsList.add(ev.getEvents().get(i).get(2));
eventsList.add(ev.getEvents().get(i).get(3));
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_event_info, container, false);


RecyclerView recyclerView = view.findViewById(R.id.rvEvents);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.mainActivity));
adapter = new EventsRecyclerViewAdapter(eventsList);

recyclerView.setAdapter(adapter);
return view;
}


}

最佳答案

在你的 'onBindViewHolder' 中,你在你的每个 textView 中设置相同的数据

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {

holder.keyID.setText(mData.get(position));
holder.lockID.setText(mData.get(position));
holder.eventTime.setText(mData.get(position));
holder.eventType.setText(mData.get(position));
}

在这里,您要为 holder 中的每个 textView 设置 mData.get(position)


一个解决方案是为你的 recyclerView 创建一个 Pojo 类

Create Pojo Event class

class Event{
public String keyID;
public String lockID;
public String eventTime;
public String eventType;
}

Create a list of 'Event' in your fragment

public class KeyEvents extends Fragment {


public static KeyInfo newInstance() {
KeyInfo fragment = new KeyInfo();
return fragment;
}

EventsRecyclerViewAdapter adapter;

ArrayList<Event> eventsList;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventsOperationHandler ev = new EventsOperationHandler();

eventsList = new ArrayList<>();

for (int i=0; i<ev.getEvents().size(); i++) {
Event event = new Event();

event.keyID = ev.getEvents().get(i).get(0);
event.lockID = ev.getEvents().get(i).get(1);
event.eventTime = ev.getEvents().get(i).get(2);
event.eventType = ev.getEvents().get(i).get(3);

eventsList.add(event);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_event_info, container, false);


RecyclerView recyclerView = view.findViewById(R.id.rvEvents);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.mainActivity));
adapter = new EventsRecyclerViewAdapter(eventsList);

recyclerView.setAdapter(adapter);
return view;
}


}

Modify your recyclerView to handle to 'Event' class

public class EventsRecyclerViewAdapter extends RecyclerView.Adapter<EventsRecyclerViewAdapter.ViewHolder> {

private List<Event> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

// data is passed into the constructor
public EventsRecyclerViewAdapter(List<Event> data) {
this.mInflater = LayoutInflater.from(MainActivity.mainActivity);
this.mData = data;
}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Event event = mData.get(position);
holder.keyID.setText(event.keyID);
holder.lockID.setText(event.lockId);
holder.eventTime.setText(event.eventTime);
holder.eventType.setText(event.eventType);
}

// total number of rows
@Override
public int getItemCount() {
return mData.size();
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView keyID;
TextView lockID;
TextView eventTime;
TextView eventType;

ViewHolder(View itemView) {
super(itemView);
keyID = itemView.findViewById(R.id.keyIDTV);
lockID = itemView.findViewById(R.id.lockIDTV);
eventTime = itemView.findViewById(R.id.eventDateTV);
eventType = itemView.findViewById(R.id.eventTypeTV);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}




// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}

就是这样,这应该可以完美地工作。

关于java - 为什么用相同的数据填充 RecyclerView 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57182436/

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