根据按钮单击将不同的数据重新填充到RecyclerView上

时间:2019-07-10 09:06:21

标签: android android-recyclerview

我有2个按钮publicReportmyReport,如果我单击公共报告,它将从我的Firebase中获取所有数据,好像我单击myReport时,它将仅基于当前登录用户。目前,我正在通过2个片段并显示2个回收站视图来实现这一目标,我认为这不是很合适。适配器中是否可以通过单击按钮重新填充不同的数据? Here是我以前的帖子的链接,在该帖子中我实现了案例的片段

其中一些代码

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ReportAdapter mAdapter;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference collectionReference = db.collection("Report");
    private List<Report> mReport;
    private Button publicReport,myReport;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        publicReport = findViewById(R.id.publicReport);
        myReport = findViewById(R.id.myReport);


        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mReport = new ArrayList<>();

        myReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collectionReference.orderBy("date", Query.Direction.DESCENDING).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                            Report report = documentSnapshot.toObject(Report.class);
                            mReport.add(report);
                        }

                        mAdapter = new ReportAdapter(getApplicationContext(),mReport);

                        mRecyclerView.setAdapter(mAdapter);
                    }
                });


            }
        });

        publicReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO: POPULATE ALL REPORT DATA
            }
        });
    }
}

ReportAdapter

public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ReportHolder> {

    private Context mContext;
    private List<Report> mReports;

    public ReportAdapter(Context context, List<Report> reports){
        mContext = context;
        mReports = reports;
    }

    public void onRefreshAdapter(List<Report> reports){
        mReports = reports;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ReportHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.report_item, parent, false);
        return new ReportHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ReportHolder holder, int position) {
        String seriousLvl = "";

        Report report = mReports.get(position);
        holder.txtTitle.setText(report.getReportType());
        holder.txtDescription.setText(report.getDescription());
        holder.txtDate.setText(report.getDate());
        holder.txtReportedBy.setText(report.getReportedBy());

        seriousLvl = report.getSeriousness();

        if (seriousLvl.equals("Low")) {
            holder.seriousness.setBackgroundColor(Color.argb(100, 0, 255, 11));
        } else if (seriousLvl.equals("Medium")) {
            holder.seriousness.setBackgroundColor(Color.argb(100, 255, 220, 0));
        } else if (seriousLvl.equals("High")) {
            holder.seriousness.setBackgroundColor(Color.argb(100, 255, 0, 0));
        } else {
            Log.d("ERROR", "NEITHER: ");
        }
    }


    @Override
    public int getItemCount() {
        return 0;
    }

    public class ReportHolder extends RecyclerView.ViewHolder{

        TextView txtTitle, txtDescription, txtDate, txtReportedBy;
        CardView seriousness;

        public ReportHolder(@NonNull View itemView) {
            super(itemView);
            seriousness = itemView.findViewById(R.id.seriousness);
            txtTitle = itemView.findViewById(R.id.text_view_title);
            txtDescription = itemView.findViewById(R.id.text_view_description);
            txtDate = itemView.findViewById(R.id.text_view_date);
            txtReportedBy = itemView.findViewById(R.id.text_view_report_by);

        }
    }
}

3 个答案:

答案 0 :(得分:0)

是的

  1. 将数据存储在ArrayList中
  2. 像这样在recyclerview适配器中创建一个方法
public void onRefreshAdapter(ArrayList<GetterSetterClass> yourList) {
                       global arraylist in adapter= yourList ;
                notifyDataSetChanged();

            }

Call onRefreshAdapter() in your button click


答案 1 :(得分:0)

已解决

如@Murali Krishnan所述,我必须在报表适配器中添加onRefreshAdapter方法,并且在单击功能上时,我将首先清除ArrayList,然后读取新数据。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    publicReport = findViewById(R.id.publicReport);
    myReport = findViewById(R.id.myReport);
    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mReport = new ArrayList<>();

    myReport.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            collectionReference.orderBy("date", Query.Direction.DESCENDING).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        Report report = documentSnapshot.toObject(Report.class);

                        mReport.clear();
                        mReport.add(report);

                    }

                    mAdapter = new ReportAdapter(MainActivity.this, mReport);
                    mAdapter.onRefreshAdapter(mReport);
                    mRecyclerView.setAdapter(mAdapter);
                }
            });

        }
    });

    publicReport.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            collectionReference.whereEqualTo("reportedByUserID","K2TPzSkIjvXCI2m1QGhyYr7qWpw1").orderBy("date", Query.Direction.DESCENDING).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        Report report = documentSnapshot.toObject(Report.class);
                        mReport.clear();
                        mReport.add(report);

                    }

                    mAdapter = new ReportAdapter(MainActivity.this, mReport);
                    mAdapter.onRefreshAdapter(mReport);
                    mRecyclerView.setAdapter(mAdapter);
                }
            });
        }
    });
}

ReportAdapter

public void onRefreshAdapter(List<Report> reports){
    mReports = reports;
    notifyDataSetChanged();
}

答案 2 :(得分:0)

最简单的方法是在片段开始时将两个数据列表加载到单独的列表中,并将默认列表设置为recyclerview适配器。

当用户按下按钮时,尝试通过添加新功能来更改RecyclerView适配器的数据集,

public changeDateSet(ArrayList<Objects> aDataList){
     mCurrentDataList = aDataList;
     notifyDataSetChanged();
}

在“每个按钮单击”事件上,您使用预加载的数据集调用此函数,以便它将更新GUI。

重要:更改适配器的数据集后,必须执行notifyDataSetChanged();。否则recyclerview不会更新gui。