为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

子衿沉夜 2023-04-13 17:21:23
我正在尝试使用存储在 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 constructorpublic EventsRecyclerViewAdapter(List<String> data) {    this.mInflater = LayoutInflater.from(MainActivity.mainActivity);    this.mData = data;}// inflates the row layout from xml when needed@Overridepublic 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@Overridepublic 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@Overridepublic int getItemCount() {    return mData.size();}// stores and recycles views as they are scrolled off screenpublic 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());    }}
查看完整描述

1 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

在你的 '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));

}

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


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


创建 Pojo 事件类


class Event{

    public String keyID;

    public String lockID;

    public String eventTime;

    public String eventType;

}

在您的片段中创建“事件”列表


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;

    }



}

修改您的 recyclerView 以处理“事件”类


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);

}

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


查看完整回答
反对 回复 2023-04-13
  • 1 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信