-1

I want to create an android application that will generate a result for a statistical treatment

enter image description here

The figure 1 in the image will be the initial table that the user will see in my application; it only contains one row with three columns, and then if the user clicks the Add button it will add another row with three columns in the table. And every time that the user will add data into the table it automatically computes percentage of the data just like the sample in Figure 2. I hope you understand what I want to do. I really needed to make this kind of application I hope you can help me because I really didn’t know the logic to create a dynamic table that automatically compute the data. I will highly appreciate your help.

This is the finished output of my application

Tonyo
  • 1
  • 1

1 Answers1

0

Ok so i think what you want to do is something like:

  1. Add Empty Row
  2. On EditText change Recalculate

First the adding of the Empty Row:

private void AddRow(){
  TableLayout tl = (TableLayout) findViewById(R.id.somelayout);
  TableRow row= new TableRow(this);
  TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
  row.setLayoutParams(lp);
  EditText fruit = new EditText(this);
  EditText freq = new EditText(this);
  EditText perc = new EditText(this);
  freq.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            ReCalculate();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
    });
  row.addView(fruit);
  row.addView(freq);
  row.addView(perc);
  tl.addView(row,i);
}

private void ReCalculate(){
    float sum = 0f;
    int rows = tl.getChildCount();
    // Get the total
    for (int i = 0; i < rows; i++) {
        TableRow elem = (TableRow) tl.getChildAt(i);
        sum+= Integer.parseInt(((EditText)elem.getChildAt(1)).getText().toString());
    }
    // Recalculate every row percent
    for (int i = 0; i < rows; i++) {
        TableRow elem = (TableRow) tl.getChildAt(i);
        int amount = Integer.parseInt(((EditText)elem.getChildAt(1)).getText().toString());
        ((EditText)elem.getChildAt(1)).setText(String.valueOf(amount/sum*100));
    }
}

Edited: The button is not dynamically created so you don't need to call row.addView() for it or the OnClickListener. And the OnClickListener of the button will just call AddRow()

This may have some errors and you will need to check for empty texts but just wanted to give you an example. Hope it helps

Ernesto Ulloa
  • 482
  • 3
  • 13
  • Thank you sir for your sample code, it helps me a lot.. But, what if the percentage column will be a textview and all its value will depend on the value that we input in Frequency column? For example we inputted 13 in the frequency column so 13/Totalfrequency*100 the answer will be fetch to the textview of percentage column, so textview will display 100, is it possible? Hope you will understand it again. Thank you very much sir. – Tonyo Sep 29 '17 at 05:18
  • Yes you will need to change just this lines: `((TextView)elem.getChildAt(1)).setText(String.valueOf(amount/sum*100));` and `((TextView)elem.getChildAt(1)).getText()` and of course change the type of percentage column: `TextView perc = new TextView(this);` – Ernesto Ulloa Sep 29 '17 at 15:31
  • thank you again sir but, what about the codes for the add button? do i need to place row.addView(); and tl.addView(); to an OnClickListener? – Tonyo Oct 01 '17 at 12:06
  • Well the button does not need to be dynamic, and as far as the OnClickListener it will call the: – Ernesto Ulloa Oct 02 '17 at 14:05
  • thank you sir.. it works, it adds rows but everytime i tried to input numbers on frequency edittext it keeps crashing. – Tonyo Oct 11 '17 at 15:58
  • Good Day sir! Thank you for your help .. your sample code really helps me and i now i have my application with a dynamic table that computes .. percentage – Tonyo Oct 13 '17 at 03:17