我正在使用retrofit
我在List类型中请求的地方。
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中的列表。现在我在适配器类中获得响应,并在MainActivity中获得 提交按钮。 所以当我改变时其他日子的微调器并提交,如何从适配器获取此项并在MainActivity中传递数据?
因为在适配器类中,它具有微调器,其中项目根据微调器选择而变化。所以我需要提交更改的项目。但问题是提交按钮在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中的项目。
答案 0 :(得分:1)
尝试此方法
- 定义一个接口
醇>RecyclerViewClickListener
,用于将消息从适配器传递到Activity/Fragment
:
public interface RecyclerViewClickListener
{
public void recyclerViewListClicked(View v, int position);
onSpinnerSelected(int position, Object selection);
}
- 在
醇>Activity/Fragment
中实现interface
,并将侦听器传递给适配器:
@Override
public void recyclerViewListClicked(View v, int position){... ...}
//set up adapter and pass clicked listener this
myAdapter = new MyRecyclerViewAdapter(context, this);
- 在Adapter和ViewHolder中:
醇>
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ItemViewHolder> {
... ...
private Context context;
private static RecyclerViewClickListener itemListener;
public MyRecyclerViewAdapter(Context context, RecyclerViewClickListener itemListener) {
this.context = context;
this.itemListener = itemListener;
... ...
}
//ViewHolder class implement OnClickListener,
//set clicklistener to itemView and,
//send message back to Activity/Fragment
public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
... ...
public ItemViewHolder(View convertView)
{
super(convertView);
... ...
convertView.setOnClickListener(this);
//setting the listener in your spinner
yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> spinner, View container,
int position, long id) {
//here you have the position and spinner value selected just pass this to callback method.
itemListener .onSpinnerSelected(getAdapterPosition(),spinner.getSelectedItem())
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v)
{
itemListener.recyclerViewListClicked(v, this.getPosition());
}
}
}
答案 1 :(得分:0)
1:制作回调侦听器,并将其传递给 recyclerview 适配器类
public interface Callback{
onSpinnerSelected(int position, Object selection);
}
2:现在将此传递给您的适配器,并提供您正在使用它的活动或片段的参考。
public class RoutePlanVisitChangeAdapter extends RecyclerView.Adapter<RoutePlanVisitChangeAdapter.RoutePlanVisitChangeView> {
private List<Retailers> mList;
private Context context;
private String[] daysList = {"MON", "TUE", "WED", "THU", "FRI", "SAT"};
private Callback callback;
public RoutePlanVisitChangeAdapter(List<Retailers> list, Context context, Callback callback) {
this.mList = list;
this.context = context;
this.callback = callback;
}
//......
}
3:,现在当你的微调器被选中时,只需要调用它。
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);
//setting the listener in toDay spinner
toDay.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> spinner, View container,
int position, long id) {
//here you have the position and spinner value selected just pass this to callback method.
callback.onSpinnerSelected(getAdapterPosition(),spinner.getSelectedItem())
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
4:现在在您提供引用或初始化构造函数的活动片段中,您必须重写回调方法。
public void initialLoadingServiceList(){
//skipping service list..
RoutePlanVisitChangeAdapter adapter = new
RoutePlanVisitChangeAdapter(retailers,
RoutePlanVisitChangeActivity.this, new Callback(){
@Override
public void onSpinnerSelected(int position, Object selection){
//here you use your selected value and cast it to particular object before use.
}
});
mRecyclerView.setAdapter(adapter);
}
答案 2 :(得分:-1)
在此处输入代码
Intent intent = new Intent(context, SelectAddressActivity.class);
intent.putExtra("id", objects.getyourvariable());
context.startActivity(intent);