gpt4 book ai didi

java - 让提交按钮适用于 recyclerview 适配器和主要 Activity 中的 edittext

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:16 26 4
gpt4 key购买 nike

我正在使用 Java + RecyclerView + CardView 开发一个小型 Android 应用程序

回收器 View 工作正常但是......

我希望卡中的提交按钮从同一张卡中的 Edittext 字段获取文本并将其分配给变量。

我希望监听器像我一样是按钮的,而不是编辑文本的。

如果我要求它执行其他操作,该按钮可以正常工作,我相信问题在于从同一张卡中的 Edittext 中检索文本。

有谁知道怎么解决吗?

谢谢

我的适配器:

public class ExerciseAdapter extends RecyclerView.Adapter<ExerciseAdapter.ExerciseViewHolder> {

private Context mCtx;
private List<Exercise> mExerciselist;
private OnItemClickListener mListener;

public interface OnItemClickListener{
void onSubClick(int position);//sub button
}

public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}

public ExerciseAdapter(Context ctx, List<Exercise> exerciselist) {
mCtx = ctx;
this.mExerciselist = exerciselist;
}

@NonNull
@Override
public ExerciseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.exercise_card, null); //null is the parent
return new ExerciseViewHolder(view, mListener);

}


@Override
public void onBindViewHolder(@NonNull ExerciseViewHolder holder, int position) {

Exercise exercise = mExerciselist.get(position);


//bind title and view
holder.textViewName.setText(exercise.getName());
holder.textViewWt.setText(exercise.getWt());
holder.textViewRep.setText(exercise.getRep());

}


@Override
public int getItemCount() {
return mExerciselist.size();
}

class ExerciseViewHolder extends RecyclerView.ViewHolder {

TextView textViewName, textViewWt, textViewRep;
Button subButton; // Sub button
EditText wtIn, repIn;
String wtstr, repstr;

public ExerciseViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);

textViewName = itemView.findViewById(R.id.exname); //populate card views
textViewWt = itemView.findViewById(R.id.exwt);
textViewRep = itemView.findViewById(R.id.exrep);
subButton = itemView.findViewById(R.id.subex); //sub button
wtIn = itemView.findViewById(R.id.inexwt);
repIn = itemView.findViewById(R.id.inexrep);

wtstr = wtIn.getText().toString();
repstr = repIn.getText().toString();

subButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onSubClick(position);
}}
}
});

}
}


}

和主要 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//init views
mGYMDays = (TextView) findViewById(R.id.gymdaystext);
inputGYMnum = (EditText) findViewById(R.id.inputnum);

//load data
loadData();


//populate list
final ArrayList<Exercise> exerciselist = new ArrayList<Exercise>();
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));


//recycle view
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

//adapter
adapter = new ExerciseAdapter(this, exerciselist);
recyclerView.setAdapter(adapter);


//clicking
adapter.setOnItemClickListener(new ExerciseAdapter.OnItemClickListener() {

@Override
public void onSubClick(int position) {

Exercise exercise = exerciselist.get(position);
EditText wtIn = (EditText)findViewById(R.id.inexwt);
EditText repIn = (EditText) findViewById(R.id.inexrep);

String newwt = wtIn.getText().toString();
String newrep = repIn.getText().toString();

exercise.Wt = newwt ;
exercise.Rep = newrep;


adapter.notifyDataSetChanged();
}
});
}

最佳答案

更简洁的解决方案是通过 OnItemClickListener 接口(interface)本身传递两个 EditText 字符串。检查下面的代码,我通过接口(interface)传递字符串,然后仅在 MainActivity 中使用传递的字符串,而不是使用 position 来填充 View 等。

public class ExerciseAdapter extends RecyclerView.Adapter<ExerciseAdapter.ExerciseViewHolder> {

private Context mCtx;
private List<Exercise> mExerciselist;
private OnItemClickListener mListener;

public interface OnItemClickListener{
void onSubClick(int position, String wtInText, String repInText);//sub button
}

public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}

public ExerciseAdapter(Context ctx, List<Exercise> exerciselist) {
mCtx = ctx;
this.mExerciselist = exerciselist;
}

@NonNull
@Override
public ExerciseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.exercise_card, null); //null is the parent
return new ExerciseViewHolder(view, mListener);

}


@Override
public void onBindViewHolder(@NonNull ExerciseViewHolder holder, int position) {

Exercise exercise = mExerciselist.get(position);


//bind title and view
holder.textViewName.setText(exercise.getName());
holder.textViewWt.setText(exercise.getWt());
holder.textViewRep.setText(exercise.getRep());

}


@Override
public int getItemCount() {
return mExerciselist.size();
}

class ExerciseViewHolder extends RecyclerView.ViewHolder {

TextView textViewName, textViewWt, textViewRep;
Button subButton; // Sub button
EditText wtIn, repIn;
String wtstr, repstr;

public ExerciseViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);

textViewName = itemView.findViewById(R.id.exname); //populate card views
textViewWt = itemView.findViewById(R.id.exwt);
textViewRep = itemView.findViewById(R.id.exrep);
subButton = itemView.findViewById(R.id.subex); //sub button
wtIn = itemView.findViewById(R.id.inexwt);
repIn = itemView.findViewById(R.id.inexrep);

wtstr = wtIn.getText().toString();
repstr = repIn.getText().toString();

subButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onSubClick(position, wtstr , repstr);
}}
}
});

}
}


}

主要 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//init views
mGYMDays = (TextView) findViewById(R.id.gymdaystext);
inputGYMnum = (EditText) findViewById(R.id.inputnum);

//load data
loadData();


//populate list
final ArrayList<Exercise> exerciselist = new ArrayList<Exercise>();
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));
exerciselist.add(new Exercise("lat","32", "22"));


//recycle view
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

//adapter
adapter = new ExerciseAdapter(this, exerciselist);
recyclerView.setAdapter(adapter);


//clicking
adapter.setOnItemClickListener(new ExerciseAdapter.OnItemClickListener() {

@Override
public void onSubClick(int position, String wtInText, String repInText) {


// We get the two Strings here directly without needing to use the position of populate the view and then get the edittext and then get the text....
exercise.Wt = wtInText;
exercise.Rep = repInText;


adapter.notifyDataSetChanged();
}
});
}

关于java - 让提交按钮适用于 recyclerview 适配器和主要 Activity 中的 edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56826340/

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