如何将数据添加到用户输入列表
我在将数据添加到我的列表中有问题。 用户必须输入数据,然后在使用模型类的recycleview
中显示数据。
应用程序正常工作,但是数据未显示在recycleview
上。 在一切正常之前,我添加了一种滑动方法来从recycleview
中删除项目,然后我必须将项目数据连接到模型类。 因此,我的最大问题是如何将数据添加到列表中。 谢谢,
我认为主要问题是代码的这一部分:
private void saveInfo() {
String ingredientName = ingrName.getText().toString();
int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
list.add(new CalculatorItemModel(ingredientName,String.valueOf(ingQuant)));
在这里我的模型类
public class CalculatorItemModel {
private String ingName, ingQuantity;
public CalculatorItemModel(String ingName, String ingQuantity) {
this.ingName = ingName;
this.ingQuantity = ingQuantity;
}
public String getIngName() {
return ingName;
}
public void setIngName(String ingName) {
this.ingName = ingName;
}
public String getIngQuantity() {
return ingQuantity;
}
public void setIngQuantity(String ingQuantity) {
this.ingQuantity = ingQuantity;
}
}
,在这里我的活动
public class CalculatorActivity extends AppCompatActivity implements CallBackItemTouch{
Button btnCclcola;
TextView cancel, doneBtn, nameTV, quantityTV, resulttry;
EditText ingrName, ingrQuantity, moltiplyN;
Dialog dialog;
RecyclerView recyclerView;
List<CalculatorItemModel> list;
ConstraintLayout constraintLayout;
private CalculatorAdapter calculatorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
nameTV = findViewById(R.id.nameTV);
quantityTV = findViewById(R.id.qunatityTV);
btnCclcola = findViewById(R.id.btn_calcola);
recyclerView = findViewById(R.id.rv_calculator);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
calculatorAdapter = new CalculatorAdapter(list);
recyclerView.setAdapter(calculatorAdapter);
dialog = new Dialog(this);
btnCclcola.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int total = 0;
for (int i = 0; i < list.size(); i++) {
int total2 = Integer.parseInt(quantity.get(i));
total += total2;
Toast.makeText(CalculatorActivity.this, ""+ total, Toast.LENGTH_SHORT).show();
}
}
});
}
private void saveInfo() {
String ingredientName = ingrName.getText().toString();
int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
list.add(new CalculatorItemModel(ingredientName,String.valueOf(ingQuant)));
}
I have problem in adding data to my list.
Users have to enter the data, then show the data in a RecycleView
that use a model class.
App works fine, but the data is not shown on the RecycleView
.
Before everything was working good, then i added a Swipe method to delete the item from the RecycleView
, and I had to connect the item data to the model class.
So, my big issue is how to add data to the list.
Thanks in advance
I think the main problem is this part of code:
private void saveInfo() {
String ingredientName = ingrName.getText().toString();
int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
list.add(new CalculatorItemModel(ingredientName,String.valueOf(ingQuant)));
Here my Model Class
public class CalculatorItemModel {
private String ingName, ingQuantity;
public CalculatorItemModel(String ingName, String ingQuantity) {
this.ingName = ingName;
this.ingQuantity = ingQuantity;
}
public String getIngName() {
return ingName;
}
public void setIngName(String ingName) {
this.ingName = ingName;
}
public String getIngQuantity() {
return ingQuantity;
}
public void setIngQuantity(String ingQuantity) {
this.ingQuantity = ingQuantity;
}
}
And here my Activity
public class CalculatorActivity extends AppCompatActivity implements CallBackItemTouch{
Button btnCclcola;
TextView cancel, doneBtn, nameTV, quantityTV, resulttry;
EditText ingrName, ingrQuantity, moltiplyN;
Dialog dialog;
RecyclerView recyclerView;
List<CalculatorItemModel> list;
ConstraintLayout constraintLayout;
private CalculatorAdapter calculatorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
nameTV = findViewById(R.id.nameTV);
quantityTV = findViewById(R.id.qunatityTV);
btnCclcola = findViewById(R.id.btn_calcola);
recyclerView = findViewById(R.id.rv_calculator);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
calculatorAdapter = new CalculatorAdapter(list);
recyclerView.setAdapter(calculatorAdapter);
dialog = new Dialog(this);
btnCclcola.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int total = 0;
for (int i = 0; i < list.size(); i++) {
int total2 = Integer.parseInt(quantity.get(i));
total += total2;
Toast.makeText(CalculatorActivity.this, ""+ total, Toast.LENGTH_SHORT).show();
}
}
});
}
private void saveInfo() {
String ingredientName = ingrName.getText().toString();
int ingQuant = Integer.valueOf(ingrQuantity.getText().toString());
list.add(new CalculatorItemModel(ingredientName,String.valueOf(ingQuant)));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
calculatorAdapter.notifyDataSetchanged()
在将项目添加到列表之后。Call
calculatorAdapter.notifyDataSetChanged()
after adding item to the list.