我使用的是retrofit
,其中我有列表类型的请求。
public RequestBody(String msisdn, List<RoutePlanDayRequests> routePlanDayRequests ) {
this.msisdn = msisdn;
this.routePlanDayRequests = routePlanDayRequests;
}
所以这个RoutePlanDayRequests有以下pojo类。
public class RoutePlanDayRequests {
private String status;
private String oldDay;
private String neweDay;
public String getOldDay() {
return oldDay;
}
public void setOldDay(String oldDay) {
this.oldDay = oldDay;
}
public String getNeweDay() {
return neweDay;
}
public void setNeweDay(String neweDay) {
this.neweDay = neweDay;
}
}
所以我调用了一个web服务来显示adapter中的列表。现在我在adapter类中得到了响应,并在MainActivity中得到了***提交按钮。***所以当我更改其他日子的微调器并提交时,如何从adapter中获得这些项目并在MainActivity中传递数据?
因为在适配器类中,它有spinner,其中项目根据spinner选择进行更改。所以这些项目在更改时需要提交。但问题是提交按钮在MainActivity中。那么如何在MainActivity中传递这些项目值?
MainActivity.class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setcontentView(R.layout.main);
initialLoadingServiceList(); //this is to load initial items
submitChangeRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//here I have been strucked.
updateServiceList();
}
});
private void updateServiceList() {
List<RoutePlanDayRequests> request = null;
//strucked to get the spinner values to pass in request.
RetrofitInstance.getInstance(this)
.create(RoutePlanService.class)
.getSaveRoutePlanDay(new SaveDayRequest(new RequestBody("",request)) //here I need to pass the changed item values from adapter to here.
}
}
//Hope you got it my requirement.
public void initialLoadingServiceList(){
//skipping service list..
RoutePlanVisitChangeAdapter adapter = new RoutePlanVisitChangeAdapter(retailers, RoutePlanVisitChangeActivity.this);
mRecyclerView.setAdapter(adapter);
}
}
适配器类别:
public class RoutePlanVisitChangeAdapter extends RecyclerView.Adapter<RoutePlanVisitChangeAdapter.RoutePlanVisitChangeView> {
private List<Retailers> mList;
private Context context;
private String[] daysList = {"MON", "TUE", "WED", "THU", "FRI", "SAT"};
public RoutePlanVisitChangeAdapter(List<Retailers> list, Context context) {
this.mList = list;
this.context = context;
}
@Override
public RoutePlanVisitChangeView onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.route_plan_visit_change_row, parent, false);
return new RoutePlanVisitChangeView(view);
}
@Override
public void onBindViewHolder(RoutePlanVisitChangeView holder, int position) {
Retailers routePlanVisitChange = mList.get(position);
holder.retailerName.setText(routePlanVisitChange.getName());
holder.retailerNumber.setText(routePlanVisitChange.getMsisdn());
holder.fromDay.setText(routePlanVisitChange.getRoutePlanDayOfWeek());
String profileUrl = routePlanVisitChange.getProfileImage().getImageUrl();
Glide.with(context)
.load(profileUrl)
.placeholder(R.drawable.ic_default_profile)
.error(R.drawable.ic_default_profile)
.into((holder.profileImage));
String getRoutePlanDay = routePlanVisitChange.getRoutePlanDayOfWeek().toUpperCase();
List<String> asSet = new ArrayList<String>(Arrays.asList(daysList));
asSet.remove(getRoutePlanDay);
String[] newDayList = asSet.toArray(new String[]{});
ArrayAdapter updatedData = new ArrayAdapter(context, android.R.layout.simple_spinner_item, newDayList);
updatedData.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.toDay.setAdapter(updatedData); //so now i changed the values in spinner. I need to update in button click in main activity
String selectedSpinner = holder.toDay.getSelectedItem().toString();
}
@Override
public int getItemCount() {
if (mList == null)
return 0;
return mList.size();
}
public class RoutePlanVisitChangeView extends RecyclerView.ViewHolder {
public TextView retailerName;
public TextView retailerNumber;
public AppCompatButton fromDay;
public Spinner toDay;
public CircleImageView profileImage;
public RoutePlanVisitChangeView(View view) {
super(view);
retailerName = (TextView)
view.findViewById(R.id.recycle_txt_acc_name);
retailerNumber = (TextView)
view.findViewById(R.id.recycle_txt_acc_num);
fromDay = (AppCompatButton) view.findViewById(R.id.from_day);
toDay = (Spinner) view.findViewById(R.id.to_day);
profileImage = (CircleImageView) view.findViewById(R.id.recycle_profile);
//toDay = (AppCompatButton) view.findViewById(R.id.to_day);
}
}
public Retailers getItem(int position) {
return mList.get(position);
}
}
}
请建议/解决我的问题,请。如何从适配器类中获取MainActivity中的项目。
3条答案
按热度按时间x4shl7ld1#
1:做一个回调监听器,传递给recyclerview*适配器**类
**2:**现在像这样将其传递给适配器,并给予您正在使用它的Activity或片段的引用。
**3:**现在,当您的微调器被选中时,只需调用此。
**4:**现在在你的Activity片段中,你已经给出了引用或者初始化了一个构造函数,你必须覆盖回调方法。
qjp7pelc2#
试试这个方法
1.定义一个接口
RecyclerViewClickListener
,用于将消息从适配器传递到Activity/Fragment
:1.在
Activity/Fragment
中实现interface
,并将监听器传递给适配器:1.在适配器和ViewHolder中:
lp0sw83n3#