Android - 保存动态生成的TextView

时间:2014-12-30 07:53:42

标签: java android android-layout

我正在尝试制作待办事项列表应用。屏幕旋转时,将删除所有动态添加的文本视图。文本视图会添加到线性布局中的LinearLayout中,位于添加按钮的正上方。

MainActivity.Java

public class MainActivity extends Activity {
    private LinearLayout mLayout;
    private LinearLayout ListItems;
    static final String counter_value = "int_value";
    static final String toDoList_value = "toDolist";
    private EditText mEditText;
    private Button mButton;
    private int counter;

    private ArrayList<String> toDoList;
    private ArrayList<String> keys;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //This makes it so that the keyboard appears only after you tap the EditText. Stackoverflow question by fixEdd Android on-screen keyboard auto popping up
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        // end code

        if (toDoList != null){
            System.out.println("Success!");
            for(int i = 0; i< toDoList.size(); i++){
                System.out.println(toDoList.get(i));
                ListItems.addView(createNewTextView(toDoList.get(i)));
            }
        }
        else{
            System.out.println("Nope!");
            toDoList = new ArrayList<String>();

        }

        counter = 1;
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        ListItems = (LinearLayout) findViewById(R.id.listItems);
        mEditText = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(onClick());
        TextView textView = new TextView(this);
        textView.setText("New text");
    }

    private View.OnClickListener onClick() {
        return new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                ListItems.addView(createNewTextView(mEditText.getText().toString()));
            }
        };
    }

    private TextView createNewTextView(String text) {
        final RadioGroup.LayoutParams lparams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setFreezesText(true);
        textView.setText(counter + ":" + text);
        textView.setId(counter);
        System.out.println(textView.getId());

        toDoList.add(text);

        counter++;
        return textView;
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){

        super.onRestoreInstanceState(savedInstanceState);

        counter = savedInstanceState.getInt(counter_value);
        toDoList = savedInstanceState.getStringArrayList("key");

        //REad values from the savedInstanceState" -object and put them in your textview
    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        // Save the values you need from your textview into "outSTate" -object
//        outState.putParcelableArrayList("key", toDoList);

        outState.putInt(counter_value, counter);
        outState.putStringArrayList("key", toDoList);
        super.onSaveInstanceState(outState);
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayout"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:id="@+id/listItems"></LinearLayout>

    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add+"
        />
</LinearLayout>

此外,我不确定读取视图是否是最好的方法。这似乎是浪费操作。有没有办法保持文本视图到位?

2 个答案:

答案 0 :(得分:1)

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){

    super.onRestoreInstanceState(savedInstanceState);

    counter = savedInstanceState.getInt(counter_value);
    toDoList = savedInstanceState.getStringArrayList("key");

    // Add this for-loop to restoring your list
    for(String str : toDoList){
        ListItems.addView(createNewTextView(str));
    }
}

但是,在我看来,这不是一个好方法。最好使用ListView

答案 1 :(得分:0)

如果使用带有适配器的ListView实现待办事项列表,则会更好。网上有很多教程,例如从this one开始。