摘要
我在RecycleView中有一个带有EditText元素的CardView,我希望获得用户输入而无需他单击按钮或按Enter,并将其与预定义的字符串进行比较。
我已经尝试了许多教程,甚至在stackOverflow中都给出了答案,但是它似乎仍然不起作用。我的想法是在Adapter类中实现TextWatcher以获取输入数据。到目前为止,这是我迄今为止的尝试(如果我删除所有相关的EditText内容,则代码可以正常运行)。贝娄是我的尝试:
MyAdapter代码
public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.MyViewHolder>{
private Context mContext;
private List<CardAudioGrid> mList;
private OnItemClickListener mListener;
private int nowPlaying = -1;
private String ans;
public interface OnItemClickListener{
void onPlayClick (int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public MyAdapter2(Context mContext, List<CardAudioGrid> mList) {
this.mContext = mContext;
this.mList = mList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.audiocard, viewGroup, false);
//when I try to run the code, it shows the error: ...MyAdapter2.MyViewHolder is not an enclosing class
return new MyViewHolder(view, mListener, new MyViewHolder.MyCustomEditTextListener());
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.audioCardBackground.setImageResource(mList.get(position).getCardBackground());
...
holder.myCustomEditTextListener.updatePosition(holder.getLayoutPosition());
}
@Override
public int getItemCount() {
return mList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView mediaPlayer, audioCardBackground;
EditText toonName;
MyCustomEditTextListener myCustomEditTextListener;
public MyViewHolder(View itemView, final OnItemClickListener listener, MyCustomEditTextListener myCustomEditTextListener) {
super(itemView);
itemView.findViewById(R.id.cardbackgroundId);
...
this.myCustomEditTextListener = myCustomEditTextListener;
mediaPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener!=null){
int position = getLayoutPosition();
nowPlaying = position;
if (position != RecyclerView.NO_POSITION){
listener.onPlayClick(position);
notifyDataSetChanged(); //restarts the views
}else{
Toast.makeText(mContext, "error", Toast.LENGTH_LONG);
}
}
}
});
}
//I want the EditText to watch what the user is typing. Every change should be compared
//to the names string given from when adding mList items
private class MyCustomEditTextListener implements TextWatcher{
private int position;
public void updatePosition(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ans = s.toString().toLowerCase();
if (ans == mList.get(position).getName()){
Toast.makeText(mContext, "Correct", Toast.LENGTH_LONG);
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
}
}
我的尝试错误在上面的代码中进行了注释。我应该如何实施这个想法?
我目前正在学习如何正确管理android,因此,如果您解释这些步骤的解决方案,我将不胜感激。
答案 0 :(得分:0)
应用此:
toonName.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
toonName.removeTextChangedListener(textWatcher);
toonName.addTextChangedListener(textWatcher);
} else {
toonName.removeTextChangedListener(textWatcher);
}
});
答案 1 :(得分:0)
在onTextChanged()
内
使用
ans = s.toString().toLowerCase();
if (ans.equals(mList.get(position).getName())){
Toast.makeText(mContext, "Correct", Toast.LENGTH_LONG);
}
答案 2 :(得分:0)
这是在单击按钮时获取所有Recyerview列表项数据的代码
integerArrayList= my array of data which i have passed to adapter
ArrayList<FormDataItems> formDataItemsArrayList = new ArrayList<>();
for (int i = 0; i < integerArrayList.size(); i++) {
View view = rvForm.getChildAt(i);
EditText etName = (EditText) view.findViewById(R.id.etName);
EditText etMobile = (EditText) view.findViewById(R.id.etMobile);
RadioGroup rgGender = (RadioGroup) view.findViewById(R.id.rbGender);
String strName = etName.getText().toString();
String strNumber = etMobile.getText().toString();
//add in to array for further use
FormDataItems formDataItems = new FormDataItems(strName, gender, strNumber);
formDataItemsArrayList.add(formDataItems);
}
在单击按钮时编写此代码,并且元素ID与recyclerview cell / item.xml相同