我创建了一个用于学习的迷你App,
当任何用户在EditText中给出任何数字并单击提交时,则TextView& EditText基于给定的Digit动态创建。
创建新的EditText后,用户在EditText中填充数据并单击提交按钮...
当用户点击提交数据时,必须传递新意图并在其中显示该数据。
我的问题是,当我点击提交按钮时,
仅显示上一个EditText值...
我该怎么办?
提前致谢...!!!
这是我的代码......
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);
}
}
}
答案 0 :(得分:2)
My Problem is that when I click on submit button,
only Last EditText Value is display...
因为,您使用此密钥覆盖for循环中的数据:
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData", items[j]);
data.putExtra("size", str);
}
之后,使用索引ID检索直到j = size。
您可以在key中使用index或传递String数组本身:
方法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]);
}
和
int size = Integer.parseInt(getIntent().getStringExtra("size"));
for (int j = 0; j < size; j++) {
Intent intent = getIntent();
String str = intent.getStringExtra("edData"+j);
}
方法2:
在intent中传递String数组,稍后在第二个活动中检索它:
intent.putExtra("strings", myStringArray);
和
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
希望这有帮助。
答案 1 :(得分:1)
请在下面查看您每次更新的价值,这就是为什么它只显示最后一位数字。
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);
}
}
}
答案 2 :(得分:0)
您可以使用“共享首选项”存储值, 稍后您可以从共享首选项中检索。
写入共享首选项
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
从共享首选项中读取
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);
签入Google API Shared - Preferences
答案 3 :(得分:-1)
在data.putExtra
循环size
之外的edData
和for
键移动data.putExtra
,就像具有唯一键的地图一样
// 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);
现在将edData
作为字符串数组而不是来自Bundle
的字符串。