Search code examples
javaandroidarraylistuser-input

How to add data to list from user input


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)));

}

Solution

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