我在一个活动中创建动态文本视图,其中包含webservice返回的一些字段。当用户想要编辑某些数据时,他将被发送到另一个活动。当他点击“后退”按钮时,我想更新上一个活动中的数据,但我不知道正确的方法。我使用onRestart方法而不是onResume
这是我的代码:
我的第一个显示数据的活动:
public class QueryActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.query);
new WSData() {
@Override
public void onPostExecute(final ArrayList<ArrayList<String>> result) { int nbTextView = result.size();
TextView[] textViewArray = new TextView[nbTextView];
for (int i = 0; i < nbTextView; i++) {
// Horizontal Layout to have the accountName and a button to
// edit on the same line
LinearLayout editLayout = new LinearLayout(
QueryActivity.this);
Button edit = new Button(QueryActivity.this);
edit.setBackgroundDrawable(getResources().getDrawable(
R.drawable.editer));
editLayout.removeAllViewsInLayout();
editLayout.setOrientation(LinearLayout.HORIZONTAL);
dynamicWindow.addView(editLayout,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
textViewArray[i] = new TextView(QueryActivity.this);
// retrieve every field from the result
final String accountId = result.get(i).get(0);
final String accountName = result.get(i).get(1);
final String phone = result.get(i).get(2);
final String industry = result.get(i).get(3);
final String website = result.get(i).get(4);
// display only the accountName
textViewArray[i].setText(accountName);
textViewArray[i].setTextColor(0xFFFFFFFF);
textViewArray[i].setGravity(Gravity.CENTER_VERTICAL);
textViewArray[i].setVisibility(View.VISIBLE);
edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(QueryActivity.this,
EditAccountActivity.class);
startActivity(intent);
}
});
}.execute(sessionName, null, "getAccountInfo");
}
@Override
public void onRestart() {
super.onRestart();
}
我的其他活动,我想在点击“返回”时更新我的数据:
public class EditAccountActivity extends Activity {
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
答案 0 :(得分:1)
When he clicks on the "back" button i want to update data in the previous activity
1-使用startActivityForResult()启动活动2。
2覆盖第二个Activity中的后退键事件并设置intent和Result_OK
3-获取第一个活动中onActivityForResult的数据
答案 1 :(得分:0)
活动1:
public void onCreate(Bundle savedInstanceState) {
...
for (int i = 0; i < nbTextView; i++) {
...
textViewArray[i].setId(Integer.parseInt(accountIdValue
.substring(3)));
...
}}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
TextView text = (TextView) findViewById(Integer.parseInt(data
.getStringExtra("accountId")));
// update the accountName
String changedAccountName = data.getStringExtra("accountname");
if (changedAccountName != null) {
text.setText(data.getStringExtra("accountname"));
}
活动2:
Intent intent = new Intent();
for (Iterator<Map.Entry<String, String>> field = fieldMap
.entrySet().iterator(); field.hasNext();) {
Map.Entry<String, String> entry = field.next();
if (checkEditTextEmpty(entry.getValue())) {
intent.putExtra(entry.getKey(), entry.getValue());
allEmptyField = false;
} else {
field.remove();
}
}
intent.putExtra("accountId", accountId.substring(3));
setResult(RESULT_OK, intent);
finish();
}