java - Android - 保存动态生成的 TextView

标签 java android android-layout

我正在尝试制作一个待办事项列表应用程序。当屏幕旋转时,所有动态添加的 TextView 都会被删除。 TextView 被添加到线性布局内的 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>

此外,我不确定阅读 View 是否是执行此操作的最佳方法。看起来这是一种浪费操作。有没有办法将 TextView 保持在适当的位置?

最佳答案

@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

关于java - Android - 保存动态生成的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27701589/

相关文章:

Java 扩展集合过滤

java - 当我的方法只被调用一次时,我的方法运行了两次

android - 找不到符号类 WorkbookSettings

android - 如何使用 ItemTouchHelper.Callback 将项目拖放到水平回收 View 的边界上?

android-layout - 如何创建 Android Google Play 的 GridView ,如动态加载不同尺寸图像的小部件

java - 返回一个结果集

java - Sqlite数据库更新一行android

android - 如何创建正方形布局并将其水平居中

android - 如何根据宽度放置两个 View ?

java - 使用单独的资源导出 lwjgl 程序