简体   繁体   中英

My list always Null. What am I doing wrong?

I am doing some kind of Medication Reminder App for our school and I am trying to create an Expandable RecyclerView based on a video I watch and practically followed step by step. Did everything practically the same but we created a Database instead of just fixed values. The problem I have is that after initializing in the Main Activity, my list value is always Null and that my adapter is returning a NULL. What am I doing wrong?

Here is my code.

MediReminders.java I've decided to remove this because its just a bunch of getters and setters

MedAdapters.java

public class MedAdapters extends RecyclerView.Adapter<MedAdapters.MediRemindersVH> {

List<MediReminders> MedList;

public MedAdapters(List<MediReminders> MedList) {
    this.MedList = MedList;
}

@NonNull
@Override
public MedAdapters.MediRemindersVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
    return new MediRemindersVH(view);
}

@Override
public void onBindViewHolder(@NonNull MedAdapters.MediRemindersVH holder, int position) {

    MediReminders mediReminders = MedList.get(position);
    holder.med_nameTxt.setText(mediReminders.getMed_name());
    holder.med_typeTxt.setText(mediReminders.getMed_type());
    holder.fromdatedetailTxt.setText(mediReminders.getFromDatedetail());
    holder.todatedetailTxt.setText(mediReminders.getToDatedetail());
    holder.timedetailTxt.setText(mediReminders.getTimedetail());
    holder.whendetailTxt.setText(mediReminders.getWhendetail());
    holder.notesdetailTxt.setText(mediReminders.getNotesdetail());

    boolean isExpandable = MedList.get(position).isExpandable();
    holder.expandableLayout.setVisibility(isExpandable ? View.VISIBLE : View.GONE);

}

@Override
public int getItemCount() {
    return MedList.size();
}

public class MediRemindersVH extends RecyclerView.ViewHolder {

    TextView med_nameTxt, med_typeTxt, fromdatedetailTxt, todatedetailTxt, timedetailTxt, whendetailTxt, notesdetailTxt;
    LinearLayout linearLayout;
    RelativeLayout expandableLayout;



    public MediRemindersVH(@NonNull View itemView) {
        super(itemView);

        med_nameTxt = itemView.findViewById(R.id.medname);
        med_typeTxt = itemView.findViewById(R.id.auto_complete_txt);
        fromdatedetailTxt = itemView.findViewById(R.id.fromdate);
        todatedetailTxt = itemView.findViewById(R.id.todate);
        timedetailTxt = itemView.findViewById(R.id.datetime);
        whendetailTxt = itemView.findViewById(R.id.radioGroup);
        notesdetailTxt = itemView.findViewById(R.id.notes);

        linearLayout = itemView.findViewById(R.id.linear_layout);
        expandableLayout = itemView.findViewById(R.id.expandable_layout);

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MediReminders mediReminders = MedList.get(getAdapterPosition());
                mediReminders.setExpandable(!mediReminders.isExpandable());
                notifyItemChanged(getAdapterPosition());
            }
        });
    }
}

}

MainActivity.java

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;

List<MediReminders> MedList;
MedAdapters adapter = new MedAdapters(MedList);

ImageButton addNewButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    recyclerView = findViewById(R.id.MedList);
    
        setContentView(R.layout.activity_main_list);
        initData();
        setRecyclerView();
    }


    private void setRecyclerView () {

        //problem is specifically this one...
        recyclerView.setAdapter(adapter);
        
        recyclerView.setHasFixedSize(true);
    }


    private void initData () {
        MedList = new ArrayList<>();

        MedList.add(new MediReminders("Paracetamol", "Tablet", "Apr 26, 2022", "May 9,2022", "6:30 PM", "Before Meal", "Take in water first"));
    }
}

First, Try to check your application with Fixed hard coded values. If it's working fine.

Second, run and check your database query on some dbms tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM