Create dynamic table row based on input

  • Replies:1
Senthil Kumar.P
  • Forum posts: 10

Mar 23, 2021, 8:44:18 PM via Website

Dear Team,

I am creating a dynamic table using 2 activities ( Reading & AggreAscendingorderr). In my activity Reading, I am having two inputs (Input1 & Input2). Input 1 is EditText and input2 is dropdown selection. On my table having 3 columns (Column1 is Textview, Column2 is Exittext & Column3 is textview) and the rows will create based on the values entered and selected.

For example input1 = 1000 and input2 = 20. So my total no of rows is 1000/20 =50 rows will create automatically.

my input two selection is 2,5,10&20.

My Reading activity code is

but1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendData();
        }

        private void sendData() {

// send maximum and interval value to aggregate ascending
if (editText1.getEditText().getText().toString().trim().isEmpty())
{
editText1.setError("Enter Weight");
}
else if (editText2.getEditText().getText().toString().trim().isEmpty())
{
editText2.setError("Select Interval");
}
else
{
Intent z = new Intent(Reading.this, AggreAscendingorder.class);

                input1 = Integer.parseInt(editText1.getEditText().getText().toString().trim());
                input2 = Integer.parseInt(editText2.getEditText().getText().toString().trim());
                z.putExtra(AggreAscendingorder.TEXT, input2);
                z.putExtra(AggreAscendingorder.EDIT, input1);
                startActivity(z);

            }

        }

    });

And my AggreAscendingorder code is

package com.example.schwingstetterindiapvtlt;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class AggreAscendingorder extends AppCompatActivity {

private TableLayout table;
private TableRow tr;
//private TextView text;
private Object Drawable;
public static final String EDIT = "EDIT";
public static final String TEXT = "TEXT";
private int input1;
private int input2;
private int x;
private Number Num;



public void init() {
    TableLayout stk = (TableLayout) findViewById(R.id.table_main);
    TableRow tbrow0 = new TableRow(this);
    TextView tv0 = new TextView(this);
    tv0.setText("Actual Values ");
    tv0.setWidth(330);
    tv0.setGravity(Gravity.CENTER);
    tv0.setTextColor(Color.WHITE);
    tv0.setTextSize(12);
    tv0.setBackgroundResource(R.drawable.border4);
    tbrow0.addView(tv0);
    TextView tv1 = new TextView(this);
    tv1.setText("Meter Values ");
    tv1.setWidth(330);
    tv1.setGravity(Gravity.CENTER);
    tv1.setTextColor(Color.WHITE);
    tv1.setTextSize(12);
    tv1.setBackgroundResource(R.drawable.border4);
    tbrow0.addView(tv1);
    TextView tv2 = new TextView(this);
    tv2.setText("Error ");
    tv2.setWidth(315);
    tv2.setGravity(Gravity.CENTER);
    tv2.setTextColor(Color.WHITE);
    tv2.setTextSize(12);
    tv2.setBackgroundResource(R.drawable.border4);
    tbrow0.addView(tv2);


    Intent z = getIntent();
    input1 = z.getIntExtra(EDIT,0);
    input2 = z.getIntExtra(TEXT,0);

    if (TEXT.equals("Custom")) {
        x = 400;
    }
    else
    {
        x = input1 / input2;
    }


    stk.addView(tbrow0);
    for (int i = 0; i < x + 1; i++) {
        TableRow tbrow = new TableRow(this);
        TextView t1v = new TextView(this);
        t1v.setText("" + i * input2);
        t1v.setTextColor(Color.BLACK);
        t1v.setGravity(Gravity.CENTER);
        t1v.setTextSize(12);
        t1v.setBackgroundResource(R.drawable.border);
        tbrow.addView(t1v);
        final EditText t2v = new EditText(this);
        t2v.setHint("");
        t2v.setTextColor(Color.BLACK);
        t2v.setGravity(Gravity.CENTER);
        t2v.setTextSize(12);
        t2v.setInputType(InputType.TYPE_CLASS_NUMBER);
        t2v.setBackgroundResource(R.drawable.border);
        tbrow.addView(t2v);
        TextView t3v = new TextView(this);
        t3v.setHint("");
        t3v.setTextColor(Color.BLACK);
        t3v.setGravity(Gravity.CENTER);
        t3v.setTextSize(12);
        t3v.setBackgroundResource(R.drawable.border);
        tbrow.addView(t3v);
        stk.addView(tbrow);
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (!t2v.getText().toString().equals("")) {
                    int temp1 = Integer.parseInt(t2v.getText().toString());
                    int temp2 = Integer.parseInt(t1v.getText().toString());
                    t3v.setText(String.valueOf(temp1 - temp2));
                }


            }


            @Override
            public void afterTextChanged(Editable s) {
                if (t2v.getText().toString().equals("")) {
                    t3v.setText("");
                }
            }
        };
        t2v.addTextChangedListener(textWatcher);

    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_aggre_ascendingorder);
    init();


}

}

The all above condition is working properly.

But I want to use "Custom" text on my input2.

When I am selecting "Custom" from input 2, the first column to change edit text and total no of rows to create 300 automatically.

Can anyone help me with how to do these chages.

Reply
Senthil Kumar.P
  • Forum posts: 10

Mar 28, 2021, 5:25:25 PM via Website

Dear experts,

Please provide a solution for my above problem

Helpful?
Reply