更改值recycleview.adapter并将其保存到mainactivity中的textview?

时间:2017-11-13 21:10:45

标签: java android android-recyclerview recycler-adapter

当我从recycleview.adapter更改值时,我想在mainactivity中将值设置为textview,该怎么做?

会是这样的, sample

回收数据从我的json中转发,因此它将是随机的和许多数据, 当我改变或添加"总赢的价值时#34;超过" 0",将显示底部的文本视图,并且"总计全部"将根据我添加的数据进行计数,完成后,我想发送"结果"值到下一个活动或发布并保存到mysql dbase,

这里是我的MainActivity代码:

    ...

private List<Details> 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);
   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();
        ...
    });

    ...

我的RecycleAdapter代码:

    ...

private Context context;
private List<Details> listDetails;

public RecyclerAdapter(Context context, List<Details> listDetails) {
    this.context = context;
    this.listDetails = listDetails;
}

@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);
    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) {
        ...
    }
}

1 个答案:

答案 0 :(得分:0)

检查代码

YourActivity ...实现RecyclerViewItemClick

     private List<Details> 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<Details> 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();

}
    }