java - 如何将新变量(来自以前的活动)添加到列表中以显示在 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 的列表视图中
解决方案
您是否尝试过使用 ArrayList 而不是字符串数组?当数组列表没有时,字符串数组具有固定大小。那可能是你的问题。
推荐阅读
- javascript - React setState 回调不会重新呈现快速切换的状态
- corda - 升级 CordaApp 以进行生产部署
- c# - 如何在属性设置器中使用 if 条件
- docker - JENKINS:构建/标记 docker Image 以使用来自 github 的 webhook 注册时标记
- azure-cognitive-search - Azure 搜索 - 像 ElasticSearch 这样的单个字段上是否有多个分析器?
- r - R 包具有不同的行为,具体取决于操作系统
- makefile - 我可以以可移植的方式有条件地在 Makefile 中定义宏吗?
- javascript - 当第二个依赖于第一个时,在另一个 API 调用中等待 API 调用?
- python - 如何在 Matplotlib 中找到轴的原点图
- html - 如何在没有服务器的情况下将 HTML 页面包含到另一个 HTML 页面中?