首页 > 解决方案 > 如何将新变量(来自以前的活动)添加到列表中以显示在 android studio 的 listview 上?

问题描述

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);


}

}

那是我在第二页上的代码,它从上一个活动中带来了变量(保存为 newThought),然后我想将其添加到 thinkList 以在 ListView 中显示,但不允许我将新变量添加到列表中。我的代码是否需要调整,或者我应该重新开始以从以前的活动中实现变量 brugth 以添加到活动 2 的列表视图中

标签: javaandroidandroid-studioandroid-studio-2.3

解决方案


您是否尝试过使用 ArrayList 而不是字符串数组?当数组列表没有时,字符串数组具有固定大小。那可能是你的问题。


推荐阅读