简体   繁体   English

如何在 android 工作室的列表视图中添加新变量(来自以前的活动)到列表中?

[英]How to add a new varible (from previous activity) to a list to display on listview in android studio?

public class Main2Activity extends AppCompatActivity {
ListView simpleList;
String[] thoughtList = {" "};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Intent intent = getIntent();
    String newThought = intent.getStringExtra(MainActivity.EXTRA_TEXT);
    thoughtList.add(newThought); //error comes herer hich wont allow add
    simpleList = (ListView)findViewById(R.id.simpleListView);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    simpleList.setAdapter(arrayAdapter);


}

} }

that is my code on second page it brings over varible (saved as newThought) from the previous activity which i then want to add to thoughtList to display in ListView but won't let me add the new varible to the list.那是我在第二页上的代码,它从上一个活动中带来了变量(保存为 newThought),然后我想将其添加到 thinkList 以在 ListView 中显示,但不允许我将新变量添加到列表中。 does my code need tweaking or should i start again to achieve varible brougth over from previous activity to add into list view on activity 2我的代码是否需要调整,或者我应该重新开始以从以前的活动中实现变量 brugth 以添加到活动 2 的列表视图中

Have you tried using an ArrayList instead of an string array?您是否尝试过使用 ArrayList 而不是字符串数组? String array has a fixed size when array list does not.当数组列表没有时,字符串数组具有固定大小。 That may be your problem.那可能是你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM