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

我正在尝试使用存储在 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());

    }

}



子衿沉夜
浏览 72回答 1
1回答

手掌心

在你的 'onBindViewHolder' 中,你在你的每个 textView 中设置相同的数据// binds the data to the TextView in each row@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {&nbsp; &nbsp; holder.keyID.setText(mData.get(position));&nbsp; &nbsp; holder.lockID.setText(mData.get(position));&nbsp; &nbsp; holder.eventTime.setText(mData.get(position));&nbsp; &nbsp; holder.eventType.setText(mData.get(position));}在这里,您将mData.get(position)设置为 holder 中的每个 textView一个解决方案是为您的 recyclerView 创建一个 Pojo 类创建 Pojo 事件类class Event{&nbsp; &nbsp; public String keyID;&nbsp; &nbsp; public String lockID;&nbsp; &nbsp; public String eventTime;&nbsp; &nbsp; public String eventType;}在您的片段中创建“事件”列表public class KeyEvents extends Fragment {&nbsp; &nbsp; public static KeyInfo newInstance() {&nbsp; &nbsp; &nbsp; &nbsp; KeyInfo fragment = new KeyInfo();&nbsp; &nbsp; &nbsp; &nbsp; return fragment;&nbsp; &nbsp; }&nbsp; &nbsp; EventsRecyclerViewAdapter adapter;&nbsp; &nbsp; ArrayList<Event> eventsList;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; EventsOperationHandler ev = new EventsOperationHandler();&nbsp; &nbsp; &nbsp; &nbsp; eventsList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<ev.getEvents().size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Event event = new Event();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.keyID = ev.getEvents().get(i).get(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.lockID = ev.getEvents().get(i).get(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.eventTime = ev.getEvents().get(i).get(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.eventType = ev.getEvents().get(i).get(3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eventsList.add(event);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.fragment_event_info, container, false);&nbsp; &nbsp; &nbsp; &nbsp; RecyclerView recyclerView = view.findViewById(R.id.rvEvents);&nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.mainActivity));&nbsp; &nbsp; &nbsp; &nbsp; adapter = new EventsRecyclerViewAdapter(eventsList);&nbsp; &nbsp; &nbsp; &nbsp; recyclerView.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }}修改您的 recyclerView 以处理“事件”类public class EventsRecyclerViewAdapter extends RecyclerView.Adapter<EventsRecyclerViewAdapter.ViewHolder> {private List<Event> mData;private LayoutInflater mInflater;private ItemClickListener mClickListener;// data is passed into the constructorpublic EventsRecyclerViewAdapter(List<Event> data) {&nbsp; &nbsp; this.mInflater = LayoutInflater.from(MainActivity.mainActivity);&nbsp; &nbsp; this.mData = data;}// inflates the row layout from xml when needed@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {&nbsp; &nbsp; View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);&nbsp; &nbsp; return new ViewHolder(view);}// binds the data to the TextView in each row@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {&nbsp; &nbsp; Event event = mData.get(position);&nbsp; &nbsp; holder.keyID.setText(event.keyID);&nbsp; &nbsp; holder.lockID.setText(event.lockId);&nbsp; &nbsp; holder.eventTime.setText(event.eventTime);&nbsp; &nbsp; holder.eventType.setText(event.eventType);}// total number of rows@Overridepublic int getItemCount() {&nbsp; &nbsp; return mData.size();}// stores and recycles views as they are scrolled off screenpublic class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {&nbsp; &nbsp; TextView keyID;&nbsp; &nbsp; TextView lockID;&nbsp; &nbsp; TextView eventTime;&nbsp; &nbsp; TextView eventType;&nbsp; &nbsp; ViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; keyID = itemView.findViewById(R.id.keyIDTV);&nbsp; &nbsp; &nbsp; &nbsp; lockID = itemView.findViewById(R.id.lockIDTV);&nbsp; &nbsp; &nbsp; &nbsp; eventTime = itemView.findViewById(R.id.eventDateTV);&nbsp; &nbsp; &nbsp; &nbsp; eventType = itemView.findViewById(R.id.eventTypeTV);&nbsp; &nbsp; &nbsp; &nbsp; itemView.setOnClickListener(this);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());&nbsp; &nbsp; }}// parent activity will implement this method to respond to click eventspublic interface ItemClickListener {&nbsp; &nbsp; void onItemClick(View view, int position);}就是这样,这应该可以完美地工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java