简体   繁体   中英

Assign edittext ids programmatically and calculate

Beginner here so sorry for the lack of skill. I'm trying to make an app that dynamically add edittext's and multiply them(rows) and then add the totals in a grand total. I managed to make it dynamically add edittexits,make it scroll,reset but i'm trying to figure out how to assign id's to every textedit and then multiply the values(from the two edit texts on the rows) and then sum all the results in a grand total.I think i know how to set id's for every edittext( with setid(i) with a for) but i can't find a way to I think i found a way but it's a bit hard to try just to possibly fail so i'm asking you guys for help. There has to be an easier way that i don't know,since i'm an absolute beginner. Please give me some alternatives if available and examples of how i can apply them. Thanks in advance !

Here's my code so far :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void produsnou(View v) {
    LinearLayout l1 = (LinearLayout) findViewById(R.id.layout1);
    EditText et = new EditText(this);
    et.setHint("Produs");
    l1.addView(et);
    int count = 4;

    int i = 4;
    for (i = 4; i < count; i++)
        ;
    LinearLayout l2 = (LinearLayout) findViewById(R.id.layout2);
    EditText et2 = new EditText(this);
    et2.setHint("Cantitate");
    et2.setInputType(InputType.TYPE_CLASS_NUMBER
            | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    l2.addView(et2);
    et2.setId(i);

    LinearLayout l3 = (LinearLayout) findViewById(R.id.layout3);
    EditText et3 = new EditText(this);
    et3.setHint("Pret");
    et3.setInputType(InputType.TYPE_CLASS_NUMBER
            | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    l3.addView(et3);
    et3.setId(i + 1);

    count = count++;

    RelativeLayout l4 = (RelativeLayout) findViewById(R.id.layout4);

}

public void reload(View v) {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Create a List and push each EditText you have created into the List. Once the list is completed, just go all over it and make the calculations.

Here's a sample code:

List<EditText> someList = new ArrayList<EditText>();

//Let's say we'd like to add 10 EditTexts
for(int i = 0; i < 10; i++){
    EditText t1 = new EditText(); //The EditText you'd like to add to the list
    t1.setText("lol"); //Give the EditText the value 'lol'
    someList.add(t1); //Add the EditText to the list
}

//Go over the list and get the values
for(EditText t : someList){
    String val = t.getText().toString(); //Get the value for the temp EditText variable t
}

In theory it should work, yet I haven't checked it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM