Auto save dynamically created table row exit text values.

  • Replies:5
  • OpenNot stickiedUnanswered
  • Forum posts: 10

Apr 10, 2021, 2:07:38 PM via Website

Deat Team,

I want to save the values entered on a dynamically created table row edit text.

SharedPreferences Prefs1 = PreferenceManager.getDefaultSharedPreferences(this);

        String s = Prefs1.getString("s1", "");
        t2v.setText(s);

         TextWatcher textWatcher1 = 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) {


                s1 = t2v.getText().toString();


                //To Save the data
                SharedPreferences Prefs1 = PreferenceManager.getDefaultSharedPreferences(AggreAscendingorder.this);
                SharedPreferences.Editor editor = Prefs1.edit();

                editor.putString("s1", s1);

                editor.apply();

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        };
        t2v.addTextChangedListener(textWatcher1);

Using my above code, when I am editing any values, it saves the last edited value on all the rows.

Can anyone correct where I am doing the mistake

Reply
  • Forum posts: 1,387

Apr 11, 2021, 8:57:17 AM via Website

You'd better upload the screenshot of your app in order for us to know the UI layout of your app and which the variable of t2v stands for.

Download size < 0.07 MB. But also Accurate, Ad-Free & Full-Free.
The minimalist app available on Google Play https://goo.gl/ws42fN
Your 5-star is appreciated. Blog : https://okblackcafe.blogspot.com

Helpful?
Reply
  • Forum posts: 19

Apr 11, 2021, 10:57:27 PM via Website

Awesome information, thank you so much

Helpful?
Reply
  • Forum posts: 10

Apr 12, 2021, 7:27:04 PM via Website

Not able to upload the picture.

My entire code is

package com.example.schwingstetterindiapvtlt;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.preference.PreferenceManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
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.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;

public class AggreAscendingorder extends AppCompatActivity {

private TableLayout table;
private TableRow tr;
private DrawerLayout drawerLayout;
//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;
String s1;
EditText ed1;



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, 1);
    input2 = z.getIntExtra(TEXT, 1);

    if (input2 == 0) {
        x = 400;
    } else {
        x = input1 / input2;
    }


    stk.addView(tbrow0);
        for (int i = 0; i < x + 1; i++) {
            TableRow tbrow = new TableRow(this);
            final EditText t1v = new EditText(this);
            if (input2 == 0) {
                t1v.setHint("");
            } else {
                t1v.setText("" + i * input2);
            }

            t1v.setTextColor(Color.BLACK);
            t1v.setGravity(Gravity.CENTER);
            t1v.setTextSize(12);
            t1v.setInputType(InputType.TYPE_CLASS_NUMBER);
            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);



         SharedPreferences Prefs1 = PreferenceManager.getDefaultSharedPreferences(this);

        String s = Prefs1.getString("s1", "");
        t2v.setText(s);

         TextWatcher textWatcher1 = 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) {


                s1 = t2v.getText().toString();


                //To Save the data
                SharedPreferences Prefs1 = PreferenceManager.getDefaultSharedPreferences(AggreAscendingorder.this);
                SharedPreferences.Editor editor = Prefs1.edit();

                editor.putString("s1", s1);

                editor.apply();

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        };
        t2v.addTextChangedListener(textWatcher1);



    }
}



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

    drawerLayout = findViewById(R.id.drawer_layout);
    init();



}
public void ClickMenu(View view) {
    NewActivity.openDrawer(drawerLayout);
}

public void ClickLogo(View view) {
    NewActivity.closeDrawer(drawerLayout);
}

public void ClickHome(View view) {
    NewActivity.redirectActivity(this, NewActivity.class);
}

public void ClickAboutUs(View view) {
    recreate();
}

public void ClickExit(View view) {
    NewActivity.Exit(this);
}



@Override
protected void onPause() {
    super.onPause();
    NewActivity.closeDrawer(drawerLayout);
}

}

Pls suggest

— modified on Apr 12, 2021, 7:29:49 PM

Helpful?
Reply
  • Forum posts: 10

May 4, 2021, 9:35:11 AM via Website

Dear experts,

Please help me to solve my above problem

Helpful?
Reply