如何将数据添加到用户输入列表

发布于 2025-01-30 16:55:09 字数 2811 浏览 2 评论 0原文

我在将数据添加到我的列表中有问题。 用户必须输入数据,然后在使用模型类的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佞臣 2025-02-06 16:55:09

调用calculatorAdapter.notifyDataSetchanged()在将项目添加到列表之后。

Call calculatorAdapter.notifyDataSetChanged() after adding item to the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文