第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么用相同的數(shù)據(jù)填充 RecyclerView 行?

為什么用相同的數(shù)據(jù)填充 RecyclerView 行?

子衿沉夜 2023-04-13 17:21:23
我正在嘗試使用存儲(chǔ)在 ArrayList 中的數(shù)據(jù)來填充 RecyclerView。當(dāng) RecyclerView 加載每條數(shù)據(jù)時(shí),每行重復(fù) 4 次。我已經(jīng)嘗試了我發(fā)現(xiàn)的各種解決方案,但似乎都無法解決問題。在對(duì)“mData”中的數(shù)據(jù)進(jìn)行一些調(diào)試之后,似乎是正確的,所以這會(huì)讓我相信這個(gè)問題是“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貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊

在你的 'onBindViewHolder' 中,你在你的每個(gè) textView 中設(shè)置相同的數(shù)據(jù)


// 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)設(shè)置為 holder 中的每個(gè) textView


一個(gè)解決方案是為您的 recyclerView 創(chuàng)建一個(gè) Pojo 類


創(chuàng)建 Pojo 事件類


class Event{

    public String keyID;

    public String lockID;

    public String eventTime;

    public String eventType;

}

在您的片段中創(chuàng)建“事件”列表


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

}

就是這樣,這應(yīng)該可以完美地工作。


查看完整回答
反對(duì) 回復(fù) 2023-04-13
  • 1 回答
  • 0 關(guān)注
  • 133 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)