如何从adapter中获取项目并将其传递到activity android中?

z2acfund  于 2023-04-28  发布在  Android
关注(0)|答案(3)|浏览(204)

我使用的是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中的项目。

x4shl7ld

x4shl7ld1#

1:做一个回调监听器,传递给recyclerview*适配器**类

public interface Callback{
    onSpinnerSelected(int position, Object selection);
}

**2:**现在像这样将其传递给适配器,并给予您正在使用它的Activity或片段的引用。

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:**现在在你的Activity片段中,你已经给出了引用或者初始化了一个构造函数,你必须覆盖回调方法。

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);

}
qjp7pelc

qjp7pelc2#

试试这个方法

1.定义一个接口RecyclerViewClickListener,用于将消息从适配器传递到Activity/Fragment

public interface RecyclerViewClickListener
{

     public void recyclerViewListClicked(View v, int position);
     onSpinnerSelected(int position, Object selection);
}

1.在Activity/Fragment中实现interface,并将监听器传递给适配器:

@Override
public void recyclerViewListClicked(View v, int position){... ...}

//set up adapter and pass clicked listener this
myAdapter = new MyRecyclerViewAdapter(context, this);

1.在适配器和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());      

       }

   }

}
lp0sw83n

lp0sw83n3#

Intent intent = new Intent(context, SelectAddressActivity.class);
intent.putExtra("id", objects.getyourvariable());
context.startActivity(intent);

相关问题