单击recyclerview中的fab按钮时,我正在加载editText。我想在editText中输入的回收视图的同一行的TextView上设置Text。当前正在从第二个文本视图设置文本,并且正在设置edittext中以前的值。基本上,如果我键入";一个";在第1版文本中;一个";应该在同一行中设置为textview;两个";在第二次编辑文本中输入";两个";在第二个文本视图中。点击Fab,它应该在recyclerview中添加项目,该项目将具有edittext和TextView。edittext中的任何更改都应反映在textview中。需要做哪些更改?我正在使用模型类主要活动.java
public class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
RecyclerView recyclerView;
ArrayList<Product> productsList;
CustomAdapter adapter;
EditText editText;
String edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rec);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
productsList = new ArrayList<>();
adapter = new CustomAdapter(MainActivity.this,productsList);
recyclerView.setAdapter(adapter);
fab = findViewById(R.id.fabBtn);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText = new EditText(MainActivity.this);
edit = editText.getText().toString();
productsList.add(new Product(0, edit));
adapter.notifyItemInserted(productsList.size()-1);
}
});
}
}
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
public static ArrayList<Product> productsList,filteredList;
private Context context;
public CustomAdapter(Context context,ArrayList<Product> productsList) {
this.context = context;
this.productsList = productsList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view, which defines the UI of the list item
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.layout_rv, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Product product = productsList.get(position);
viewHolder.idtxt.setText(String.valueOf(product.getId()));
viewHolder.textView.setText(productsList.get(position).getName());
// viewHolder.txtsave.setText(product.getName());
}
@Override
public int getItemCount() {
return productsList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView idtxt;
EditText textView;
TextView txtsave;
public ViewHolder(@NonNull View itemView) {
super(itemView);
idtxt = itemView.findViewById(R.id.idttxt);
textView = itemView.findViewById(R.id.textView);
txtsave = itemView.findViewById(R.id.txtsave);
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
productsList.get(getAdapterPosition()).setName(textView.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
//viewHolder.textView.getText().toString();
}
});
for (int i = 0; i < productsList.size(); i++){
txtsave.setText(txtsave.getText() + " " + productsList.get(i).getName() +System.getProperty("line.separator"));
}
}
}
}
您需要在回调onTextChanged
中执行此操作。我认为您尝试过使用for循环来完成此操作,但这不在回调范围内,并且只有在创建ViewHolder
时才会调用,即使这样也不会影响不同的行。
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView idtxt;
EditText textView;
TextView txtsave;
public ViewHolder(@NonNull View itemView) {
super(itemView);
idtxt = itemView.findViewById(R.id.idttxt);
textView = itemView.findViewById(R.id.textView);
txtsave = itemView.findViewById(R.id.txtsave);
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
productsList.get(getAdapterPosition()).setName(charSequence);
txtsave.setText(charSequence)
}
@Override
public void afterTextChanged(Editable editable) {}
});
}
}