I created one mini App for learning,
When any user give any number in EditText and click on submit, then TextView & EditText created dynamically based on given Digit.
and After created new EditText, user fill Data in EditText and click on submit button...
When user click on submit data must pass in new intent and Display that data in it.
My Problem is that when I click on submit button,
only Last EditText Value is display...
What Can I Do?
Thanks In Advance...!!!
Here is my Code...
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter Digit Here" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DELETE" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
MainAvtivity.java
package com.example.textviewdemo;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private TextView addTv;
private EditText edt, edtAdd, edArray;
private Button add, delete, submit;
LinearLayout layout;
List<EditText> allEds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt = (EditText) findViewById(R.id.editText1);
add = (Button) findViewById(R.id.button1);
delete = (Button) findViewById(R.id.button2);
submit = (Button) findViewById(R.id.button3);
layout = (LinearLayout) findViewById(R.id.LinearLayout);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int no = Integer.parseInt(edt.getText().toString());
allEds = new ArrayList<EditText>();
for (int i = 0; i < no; i++) {
addTv = new TextView(MainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
addTv.setText("TextView " + i);
addTv.setId(i);
layout.addView(addTv);
edtAdd = new EditText(MainActivity.this);
layout.setLayoutParams(params);
allEds.add(edtAdd);
edtAdd.setText("Test" + i);
edtAdd.setId(i);
layout.addView(edtAdd);
}
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent(MainActivity.this, Display.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData", items[j]);
data.putExtra("size", str);
/*
* Toast.makeText(getApplicationContext(), items[j],
* Toast.LENGTH_SHORT).show();
*/
}
startActivity(data);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.removeAllViews();
}
});
}
}
Display.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/LinearLayout_1" >
</LinearLayout>
</LinearLayout>
Display.java
package com.example.textviewdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Display extends ActionBarActivity {
TextView getText;
LinearLayout linear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
Intent get = getIntent();
linear = (LinearLayout) findViewById(R.id.LinearLayout_1);
int size = Integer.parseInt((get.getExtras().getString("size")));
for (int i = 0; i < size; i++) {
getText = new TextView(Display.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
getText.setText(get.getExtras().getString("edData"));
linear.setLayoutParams(params);
getText.setId(i);
linear.addView(getText);
}
}
}
My Problem is that when I click on submit button,
only Last EditText Value is display...
Because, you are overwriting the data in your for loop using this key:
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData", items[j]);
data.putExtra("size", str);
}
And later, retrieve using index id till j = size.
You can either use index in key or pass String array itself:
Approach 1:
data.putExtra("size", str); //you don't need to keep this in loop as its same.
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData"+j, items[j]);
}
and
int size = Integer.parseInt(getIntent().getStringExtra("size"));
for (int j = 0; j < size; j++) {
Intent intent = getIntent();
String str = intent.getStringExtra("edData"+j);
}
Approach 2:
Pass String array in intent, and later, retrieve it in second activity:
intent.putExtra("strings", myStringArray);
and
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
Hope this helps.
Please check below you are updating the value every time that's why it is only showing last digit.
package com.example.textviewdemo;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private TextView addTv;
private EditText edt, edtAdd, edArray;
private Button add, delete, submit;
LinearLayout layout;
List<EditText> allEds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt = (EditText) findViewById(R.id.editText1);
add = (Button) findViewById(R.id.button1);
delete = (Button) findViewById(R.id.button2);
submit = (Button) findViewById(R.id.button3);
layout = (LinearLayout) findViewById(R.id.LinearLayout);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int no = Integer.parseInt(edt.getText().toString());
allEds = new ArrayList<EditText>();
for (int i = 0; i < no; i++) {
addTv = new TextView(MainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
addTv.setText("TextView " + i);
addTv.setId(i);
layout.addView(addTv);
edtAdd = new EditText(MainActivity.this);
layout.setLayoutParams(params);
allEds.add(edtAdd);
edtAdd.setText("Test" + i);
edtAdd.setId(i);
layout.addView(edtAdd);
}
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent(MainActivity.this, Display.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
/*
* Toast.makeText(getApplicationContext(), items[j],
* Toast.LENGTH_SHORT).show();
*/
}
//changes made here
data.putExtra("edData", items);
data.putExtra("size", str);
startActivity(data);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.removeAllViews();
}
});
}
}
package com.example.textviewdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Display extends ActionBarActivity {
TextView getText;
LinearLayout linear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
Intent get = getIntent();
linear = (LinearLayout) findViewById(R.id.LinearLayout_1);
int size = Integer.parseInt((get.getExtras().getString("size")));
for (int i = 0; i < size; i++) {
getText = new TextView(Display.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
String[] data = get.getExtras().getString("edData");
String textOnRequest = ";
for(int i=0 ; i < data.length ; i++){
textOnResponse += data[i];
}
getText.setText(textOnResponse);
linear.setLayoutParams(params);
getText.setId(i);
linear.addView(getText);
}
}
}
You can use Shared Preferences to store values, And later on you can retrieve from shared preferences.
Write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
Read from Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
Check in Google API Shared - Preferences
Move data.putExtra
for both size
and edData
keys outside for
loop data.putExtra
work like a Map which have a unique keys
// Prepare items Array
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
}
data.putExtra("edData", items);
data.putExtra("size", str);
Now get edData
as String Array instead of String from Bundle
.
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.