我正在使用Java + RecyclerView + CardView开发一个小型android应用
回收站视图工作正常,但是...
我希望卡片中的“提交”按钮从同一卡片中的“编辑文本”字段中获取文本,并将其分配给变量。
我希望监听器像按钮一样,而不是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);
}}
}
});
}
}
}
和主要活动:
@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();
}
});
}
答案 0 :(得分:0)
一个更巧妙的解决方案是通过EditText
接口本身传递两个OnItemClickListener
字符串。检查下面的代码,我通过接口传递字符串,然后仅在MainActivity
中使用传递的字符串,而不是使用position
来填充视图等。
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);
}}
}
});
}
}
}
主要活动:
@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();
}
});
}