Android - 使用 ArrayAdapter 一次向 ListView 添加和显示项目

标签 android listview android-listview android-asynctask android-arrayadapter

我正在使用 ArrayAdapter 将项目添加到自定义 ListView 并在我的 Android 应用程序中显示结果。我遇到的问题是 ArrayAdapter 似乎要等到所有项目都在其中才能显示 View 。也就是说,当我将项目添加到 ArrayAdapter 并调用 notifyDataSetChanged 时,它不会更新 ListView 以显示添加的项目。它会等到所有项目都添加完毕并在显示项目之前调用 GetView。

我希望它做的是在将项目添加到 ListView 后立即显示该项目。这可能吗?

我相信相关代码如下:

r_adapter = new ReminderAdapater(Activity_ContentSearch.this, R.layout.search_listitem, myList);
listView.setAdapter(r_adapter);
...
r_adapter.notifyDataSetChanged();
r_adapter.clear();
for(int i = 0; i < myList.size(); i++)
{
    r_adapter.add(myList.get(i));
    r_adapter.notifyDataSetChanged();
}

如您所见,即使我在 add 方法之后调用 notifyDataSetChanged,它实际上并没有更新 View 。完成上述循环后, View 最终更新(据我所知,这是因为在这部分代码完成之前不会调用 GetView)。

我试图重写自定义 ArrayAdapter 的添加方法,但没有成功,因为我无权访问该方法中的 View 。

欢迎任何帮助:)

巴拉

最佳答案

Android 的 UI 是单线程的。每次向适配器添加条目时,您都不会将控制权从主应用程序线程交还给 Android。因此,在您返回控制权之前,Android 没有机会显示您的条目,并且在您完全填充适配器之前,您不会这样做。

Here is an example展示了如何使用 AsyncTask 通过后台线程逐步填充 ArrayAdapter

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
*/

package com.commonsware.android.async;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.ArrayList;

public class AsyncDemo extends ListActivity {
  private static final String[] items={"lorem", "ipsum", "dolor",
                                      "sit", "amet", "consectetuer",
                                      "adipiscing", "elit", "morbi",
                                      "vel", "ligula", "vitae",
                                      "arcu", "aliquet", "mollis",
                                      "etiam", "vel", "erat",
                                      "placerat", "ante",
                                      "porttitor", "sodales",
                                      "pellentesque", "augue",
                                      "purus"};
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1,
                        new ArrayList<String>()));

    new AddStringTask().execute();
  }

  class AddStringTask extends AsyncTask<Void, String, Void> {
    @Override
    protected Void doInBackground(Void... unused) {
      for (String item : items) {
        publishProgress(item);
        SystemClock.sleep(200);
      }

      return(null);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onProgressUpdate(String... item) {
      ((ArrayAdapter<String>)getListAdapter()).add(item[0]);
    }

    @Override
    protected void onPostExecute(Void unused) {
      Toast
        .makeText(AsyncDemo.this, "Done!", Toast.LENGTH_SHORT)
        .show();
    }
  }
}

关于Android - 使用 ArrayAdapter 一次向 ListView 添加和显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2130546/

相关文章:

java - 如何使用 FusedLocation API 每 5 分钟接收一次位置更新

android - setText(getString(R.strings.whatever) 或 setText(R.strings.whatever)?

java - 将 BooleanArray 保存在 LibgGDX 首选项中

android - 致命异常:IllegalStateException无法执行 Activity 的方法

android - 如何检查 Android 中数组中存在的数据?

android - 图像未存储在缓存中

java - RecyclerView Item 不包裹 ListView 的高度

带有微调器和复选框的 Android Listview

java - ListView 打开一个新 Activity

java - 允许用户搜索并滚动到 ListView