gpt4 book ai didi

java - Recyclerview 构造函数不保存参数

转载 作者:行者123 更新时间:2023-12-01 17:11:17 26 4
gpt4 key购买 nike

这是我的 fragment 类,我在其中调用 recyclerview 适配器类构造函数并在其中传递 int 值。构造函数参数名称是 project_id。

public class pendingactivity extends Fragment {

MyViewModel myViewModel;
List<Activity_> activity_list;
CheckBox act_check;
View view;
int project_id;
int id;
Projectid_pointing_towards_activity PPta;


public pendingactivity() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView mrecyclerView;

//Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_pendingactivity, container, false);
mrecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_frag1);
act_check = (CheckBox) view.findViewById(R.id.activity_check);
myViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
myViewModel.GetAllPPTA().observe(this, new Observer<List<Projectid_pointing_towards_activity>>()
{
@Override
public void onChanged(List<Projectid_pointing_towards_activity> PPTA) {
PPta = PPTA.get(1);
project_id = PPta.getProject_id();
}
});
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
mrecyclerView.setLayoutManager(linearLayoutManager);

final activitycardAdaptor Acticityadaptor = new activitycardAdaptor(project_id);


myViewModel.GetAllActivity().observe(this, new Observer<List<Dactivity>>() {
@Override
public void onChanged(List<Dactivity> dactivities) {


id=Acticityadaptor.setActivity(dactivities);
Toast.makeText(getActivity(),"id is"+id,Toast.LENGTH_LONG).show();

}
});
mrecyclerView.setAdapter(Acticityadaptor);


return view;

}

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


}


}

下面是recyclerview适配器类代码。在这个类中project_id没有显示任何数据。问题是我通过构造函数发送数据但没有接收。 setActivity 函数返回 int 值只是为了我的测试目的,如果项目 id 有数据或没有数据,但此函数返回值为 0 但我发送 1。

public class activitycardAdaptor extends RecyclerView.Adapter<ActivityCardViewHolder> {


private Context context;
private List<Dactivity> mactivity;
private int project_id;


public activitycardAdaptor(int project_id) {
this.project_id = project_id;
}

@Override
public ActivityCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mview = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_activity, parent,
false);

return new ActivityCardViewHolder(mview);
}

@Override
public void onBindViewHolder(ActivityCardViewHolder holder, int position) {
Dactivity dactivity = mactivity.get(position);
holder.Activity_title.setText(dactivity.getName());
holder.Activity_Type.setText(dactivity.getType());
Calendar calendar = Calendar.getInstance();
String curentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
holder.Date.setText(curentDate);


}

@Override
public int getItemCount() {

return mactivity == null ? 0 : mactivity.size();


}


public int setActivity(List<Dactivity> dactivities) {
mactivity=new ArrayList<>();

for(int i=0;i<dactivities.size();i++)
{
Dactivity currentproject= dactivities.get(i);
if(project_id==currentproject.getProject_id())
{
mactivity.add(currentproject);



}
}



notifyDataSetChanged();
return project_id;

}
}

class ActivityCardViewHolder extends RecyclerView.ViewHolder {
CardView mcardview;
TextView Activity_title, Activity_Type, Date;
CheckBox activity_check;


public ActivityCardViewHolder(View itemView) {
super(itemView);

Activity_title = itemView.findViewById(R.id.Activity_name);
Activity_Type = itemView.findViewById(R.id.activity_type);
Date = itemView.findViewById(R.id.date);
activity_check = itemView.findViewById(R.id.activity_check);

mcardview = itemView.findViewById(R.id.activity_card);


}
}

请帮助我,因为我在这方面被困了大约 5 天。

最佳答案

调用此线路怎么样:

final activitycardAdaptor Acticityadaptor = new activitycardAdaptor(project_id);

这里:

myViewModel.GetAllPPTA().observe(this, new Observer<List<Projectid_pointing_towards_activity>>() 
{
@Override
public void onChanged(List<Projectid_pointing_towards_activity> PPTA) {
PPta = PPTA.get(1);
project_id = PPta.getProject_id();

//here make it member variable of your class and initialize here
Acticityadaptor = new activitycardAdaptor(project_id);
}
});

关于java - Recyclerview 构造函数不保存参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61428392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com