gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:00:42 25 4
gpt4 key购买 nike

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

事情会是这样的 sample

回收数据从我的json中检索,因此它将是随机的并且数据很多,当我更改或添加“总获胜”的值超过“0”时,底部的 TextView 将出现,并且“总计”将从我添加的数据中计数,完成后,我想发送“结果” “值到下一个 Activity 或帖子并将其保存到 mysql 数据库,

这是我的 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) {
...
}
}

最佳答案

检查代码

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

}
}

关于java - 更改值 recyclerview.adapter 并将其保存到 mainactivity 中的 textview 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47273614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com