2

我在一个活动中创建动态文本视图,其中一些字段由 Web 服务返回。当用户想要编辑一些数据时,他会被发送到另一个活动。当他单击“后退”按钮时,我想更新上一​​个活动中的数据,但我不知道正确的做法。我使用 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();
            }
    });
4

2 回答 2

1
When he clicks on the "back" button i want to update data in the previous activity

1- 使用 startActivityForResult() 启动 Activity 2。

2 覆盖2nd Activity中的后退键事件并设置intent和Result_OK

3- 在第一个活动的 onActivityForResult 中获取数据

于 2012-06-26T15:04:43.790 回答
0

我是这样做的:

活动一:

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

活动二:

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();
            }
于 2012-06-27T10:24:33.547 回答