Change value recycleview.adapter and save it to textview in mainactivity?

前端 未结 1 1348
鱼传尺愫
鱼传尺愫 2021-01-28 14:56

When I change the value from recycleview.adapter, i want to set it the value to textview in mainactivity, how to do it?

it will be like this,

the recycle data r

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 15:34

    Check the code

    YourActivity ... implements RecyclerViewItemClick

         private List
    listDetails = new ArrayList<>(); private RecyclerAdapter viewAdapter; @BindView(R.id.recyclerView) RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view); ButterKnife.bind(this); viewAdapter = new RecyclerAdapter(this, listDetails, this); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(viewAdapter); getMyData(); } ... private void getMyData() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(AppConfig.URL_MAIN) .addConverterFactory(GsonConverterFactory.create()) .build(); ... });

    RecyclerViewAdapter

    private RecyclerViewItemClick mListener;
    
        public RecyclerAdapter(Context context, List
    listDetails, RecyclerViewItemClick listener) { this.context = context; this.listDetails = listDetails; this.mListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view, parent, false); ViewHolder holder = new ViewHolder(v, mListener); return holder; } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { final Details getSet = listDetails.get(position); holder.tvName.setText(getSet.getName()); holder.numberPickerTotalWin.setValueChangedListener(new ValueChangedListener() { @Override public void valueChanged(int value, ActionEnum action) { Toast.makeText(context, position + value + " Clicked", Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return listDetails.size(); } public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ @BindView((R.id.nP)) NumberPicker numberPickerTotalWin; @BindView(R.id.tvName) TextView tvName; public ViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); itemView.setOnClickListener(this); } @Override public void onClick(View view) { if (mListener != null){ //you can access to the current position if you want -> getLayoutPosition() mListener.yourMethod(); } } public interface RecyclerViewItemClick { void yourMethod(); } }

    0 讨论(0)
提交回复
热议问题