使用动态内容刷新活动

时间:2012-08-21 09:45:47

标签: android dynamic android-activity refresh

我的活动包含一些动态内容。

对于数组的每个项目,都有不同的内容(textViews的数量,复选框,..) 用户点击“保存”按钮,然后屏幕必须刷新自己以显示下一个项目的内容。

我想知道重新加载我的活动是否是一个好习惯:

Intent refresh = new Intent(this, conflictActivity.class);
startActivity(refresh)
finish()

而不是删除布局中的所有视图等...

我认为重新加载它更容易。 还有其他办法吗?

编辑:添加了代码。

public class ConflicFieldTest extends Activity {

private int nbField;
private int nbChecked;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.conflict);

    final Button saveButton = (Button) findViewById(R.id.save);

    final LinearLayout leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
    final LinearLayout rightLayout = (LinearLayout) findViewById(R.id.rightLayout);

    final TextView fieldName = new TextView(this);
    final LinearLayout editLayoutRight = new LinearLayout(this);
    final LinearLayout editLayoutLeft = new LinearLayout(this);

    final LinearLayout fieldRight = new LinearLayout(this);
    final LinearLayout fieldLeft = new LinearLayout(this);


    final ArrayList<String> allIdInConflict = getAllIdInConflict();
    for (int i = 0; i < allIdInConflict.size(); i++) {

        new AccountSyncTask() {

            @Override
            public void onPostExecute(
                    final ArrayList<ArrayList<String>> result) {

                nbField = 0;
                nbChecked = 0;



                final Map<String, Object> fieldsInConflict = conflict
                        .conflictWithFields(memoAccountMap,
                                serverAccountMap, modifiedAccountMap);


                for (Iterator<Map.Entry<String, Object>> field = fieldsInConflict
                        .entrySet().iterator(); field.hasNext();) {
                    final Map.Entry<String, Object> entry = field.next();

                    fieldName.setText(entry.getKey() + " : ");

                    final EditText[] editTextArrayRight = new EditText[fieldsInConflict
                            .size()];
                    final EditText[] editTextArrayLeft = new EditText[fieldsInConflict
                            .size()];
                    final CheckBox[] checkboxRight = new CheckBox[fieldsInConflict
                            .size()];
                    final CheckBox[] checkboxLeft = new CheckBox[fieldsInConflict
                            .size()];

                    checkboxRight[nbField] = new CheckBox(
                            ConflicFieldTest.this);
                    checkboxLeft[nbField] = new CheckBox(
                            ConflicFieldTest.this);

                    editLayoutLeft.setGravity(Gravity.CENTER_HORIZONTAL);
                    editLayoutRight.setGravity(Gravity.CENTER_HORIZONTAL);
                    fieldRight.setGravity(Gravity.CENTER_HORIZONTAL);
                    fieldLeft.setGravity(Gravity.CENTER_HORIZONTAL);


                    editLayoutRight.setOrientation(LinearLayout.HORIZONTAL);
                    fieldRight.setOrientation(LinearLayout.VERTICAL);
                    fieldLeft.setOrientation(LinearLayout.VERTICAL);


                    rightLayout
                            .addView(
                                    editLayoutRight,
                                    new LinearLayout.LayoutParams(
                                            LinearLayout.LayoutParams.FILL_PARENT,
                                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    leftLayout
                            .addView(
                                    editLayoutLeft,
                                    new LinearLayout.LayoutParams(
                                            LinearLayout.LayoutParams.FILL_PARENT,
                                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    editTextArrayRight[nbField] = new EditText(
                            ConflicFieldTest.this);

                    editTextArrayRight[nbField].setText((String) entry
                            .getValue());
                    editTextArrayRight[nbField].setTextColor(Color.BLACK);
                    editTextArrayRight[nbField].setFocusable(false);
                    editTextArrayRight[nbField].setClickable(false);
                    editTextArrayRight[nbField].setWidth(180);
                    editTextArrayRight[nbField].setPadding(0, 10, 0, 0);

                    editTextArrayLeft[nbField] = new EditText(
                            ConflicFieldTest.this);
                    editTextArrayLeft[nbField]
                            .setText((String) serverAccountMap.get(entry
                                    .getKey()));
                    editTextArrayLeft[nbField].setTextColor(Color.BLACK);
                    editTextArrayLeft[nbField].setFocusable(false);
                    editTextArrayLeft[nbField].setClickable(false);
                    editTextArrayLeft[nbField].setWidth(180);
                    editTextArrayLeft[nbField].setPadding(0, 10, 0, 0);

                    final int i1 = nbField;

                    checkboxLeft[i1]
                            .setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {



                                }
                            });

                    checkboxRight[i1]
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {

                                }
                            });



                    editLayoutLeft.addView(fieldName);

                    editLayoutRight
                            .addView(
                                    editTextArrayRight[nbField],
                                    new LinearLayout.LayoutParams(
                                            LinearLayout.LayoutParams.WRAP_CONTENT,
                                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    editLayoutLeft
                            .addView(
                                    editTextArrayLeft[nbField],
                                    new LinearLayout.LayoutParams(
                                            LinearLayout.LayoutParams.WRAP_CONTENT,
                                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    editLayoutRight.addView(checkboxRight[nbField]);
                    editLayoutLeft.addView(checkboxLeft[nbField]);

                    nbField++;

                }

                saveButton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        //save data & update UI
            }
        }

        .execute(appState.getSessionName(), id, "getObjectOnServer");

    }
}

对不起代码,它有点脏(我删除了一些部分),我正在重做它。

1 个答案:

答案 0 :(得分:1)

int flag = 0;
public void updateUI() {
  if(flag == 0) {
     //your normal codes...
  } else {
     //display view with user given data
  }
}

oncreate中调用此功能。首先将标志设置为零。当您在textview中输入数据并勾选复选框时,请使用某个值更新标志。然后在save Button点击再次调用此功能。现在标志已设置。因此它将显示更新的UI。

我认为这会给你一个想法..