从ArrayAdapter

时间:2015-10-18 09:05:07

标签: android listview android-listview

我在其上设置了ListView和ArrayAdapter。我的ListView单行包含TextView和开始Button。单击按钮时,TextView文本将每毫秒更改一次。我为它定义了计时器,但是当我更改TextView文本时,它不会出现在我的视图中。它只在我调用notifyDataSetChanged时才会改变。但是在这种情况下,每隔毫秒调用notifyDataSetChanged,我的ListView就崩溃了。我怎么解决这个问题?除了我想说我用ViewHolder模式实现了ArrayAdapter并覆盖了所有必要的方法。

3 个答案:

答案 0 :(得分:2)

您应该使用RecyclerView它是一个灵活的视图,用于将有限元素提供到大型数据集中。因此,您可以使用它来控制在特定条件下应加载的项目数。您可以阅读有关RecyclerView here

的信息

答案 1 :(得分:1)

Harshad写道,你可以使用RecyclerView,这是最好的方法。

如果由于某种原因您不想要,可以使用以下行代码手动更新特定的列表视图行:

View convertView = listview.getChildAt(rowIndex - listview.getFirstVisiblePosition());

它比notifyDataChanged ...

快得多

答案 2 :(得分:1)

我编写了另一个解决方案,比如RecyclyerView方法void notifyItemChanged(int position),创建CustomBaseAdapter类就像这样:

public abstract class CustomBaseAdapter implements ListAdapter, SpinnerAdapter {

    private final CustomDataSetObservable mDataSetObservable = new CustomDataSetObservable();

    public boolean hasStableIds() {
        return false;
    }

    public void registerDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.registerObserver(observer);
    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.unregisterObserver(observer);
    }

    public void notifyDataSetChanged() {
        mDataSetObservable.notifyChanged();
    }

    public void notifyItemChanged(int position) {
        mDataSetObservable.notifyItemChanged(position);
    }

    public void notifyDataSetInvalidated() {
        mDataSetObservable.notifyInvalidated();
    }

    public boolean areAllItemsEnabled() {
        return true;
    }

    public boolean isEnabled(int position) {
        return true;
    }

    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }

    public int getItemViewType(int position) {
        return 0;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }
}

不要忘记为CustomAdapterClass中的mDataSetObservable变量创建一个CustomDataSetObservable类,如下所示:

public class CustomDataSetObservable extends Observable<DataSetObserver> {

    public void notifyChanged() {
        synchronized(mObservers) {
            // since onChanged() is implemented by the app, it could do anything, including
            // removing itself from {@link mObservers} - and that could cause problems if
            // an iterator is used on the ArrayList {@link mObservers}.
            // to avoid such problems, just march thru the list in the reverse order.
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onChanged();
            }
        }
    }

    public void notifyInvalidated() {
        synchronized (mObservers) {
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onInvalidated();
            }
        }
    }

    public void notifyItemChanged(int position) {
        synchronized(mObservers) {
            // since onChanged() is implemented by the app, it could do anything, including
            // removing itself from {@link mObservers} - and that could cause problems if
            // an iterator is used on the ArrayList {@link mObservers}.
            // to avoid such problems, just march thru the list in the reverse order.
            mObservers.get(position).onChanged();
        }
    }
}
在类CustomBaseAdapter上有一个方法notifyItemChanged(int position),当你想要在任何你想要的地方更新行时(从按钮点击或你想要调用该方法的任何地方),你可以调用该方法。瞧!,你的单行会立即更新..

希望这个解决方案能帮到你..