Android 中的异步任务后在 TextView 中显示项目数

Display number of items in TextView after async task in Android

我调用了一个 Web 服务,该服务获取 0-n 个项目,这些项目显示在 Android activity 的 ListView 中。

在我的 onCreate() 中我有

new GetData().execute();

它填充了一个名为 customerDataList 的对象。

并且在 onPostExecute 中我使用列表适配器来显示返回的数据。

我想在 ListView 上方显示检索到的项目数,但是如果我这样做:

new GetData().execute();
TextView numberOfItemsElm = (TextView) findViewById(R.id.number_of_items);
numberOfItemsElm.setText(customerDataList.size());
显示

0 是因为该行在执行完成之前是 运行。

有什么想法吗?

0 is displayed because that line is run before the execution has finished.

完全正确

所以,移动这些行

TextView numberOfItemsElm = (TextView) findViewById(R.id.number_of_items);
numberOfItemsElm.setText(customerDataList.size());

在您的列表中设置适配器后onPostExecute()

或者,您可以将设置文本的代码放在 onPostExecute() 中调用的方法中,以防您在 Activity.

的其他地方使用该代码

虽然,这实际上应该抛出一个 ResourceNotFoundException 因为你发送一个 int 作为参数。应该是这样的

numberOfItemsElm.setText("" + customerDataList.size());