Auto save (SharedPreferences) dynamically created table row edit text values

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

My code is

@SuppressLint("ResourceType")
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);
t1v.setId(1);
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);

SharedPreferences Prefs = PreferenceManager.getDefaultSharedPreferences(this);
String s = Prefs.getString("s1", "");
t2v.setText(s);

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

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

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

editor.putString("s1",t2v.getText().toString());

editor.apply();
}

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

}
}

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

Can anyone correct where I am doing the mistake?

Sharing is caring!

Leave a Reply