gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:04:46 25 4
gpt4 key购买 nike

我正在尝试制作一个待办事项列表应用程序。当屏幕旋转时,所有动态添加的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com