为什么在自定义ArrayAdapter中实例化新的自定义处理程序?

时间:2012-01-24 20:58:56

标签: android listview android-arrayadapter

我已经使用了一段时间了,我不确定我是在一个例子中找到这种技术,还是只是尝试了一堆东西,直到它最终起作用。我的问题是这行代码如何,

new ObjectInterfaceHandler(position, o, v);

实际上使它到达此视图的位置从ListReadyObject中侦听回调。

package com.scs.stuff;

import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListReadyObjectAdapter extends ArrayAdapter<ListReadyObject> {

    public ListReadyObjectAdapter(Context context, int textViewResourceId,
            ArrayList<ListReadyObject> lro) {
        super(context, textViewResourceId, lro);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout v;
        v = new LinearLayout(getContext());
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        vi.inflate(R.layout.list_row, v, true);

        ListReadyObject o = getItem(position);

        if (o != null) {

            TextView tt = (TextView) v.findViewById(R.id.toptext);
            TextView bt = (TextView) v.findViewById(R.id.bottomtext);

            if (tt != null) {
                tt.setText(o.getDisplayText());
            }

            if (o.isLiving()) {
                if (bt != null) {
                    // set default text to be shown
                    // until the status thread completes
                    bt.setText("---");
                }
                // start a background thread to update Display State
                o.updateStatus();
                // why does this work
                new ObjectInterfaceHandler(position, o, v);

            } else {
                if (bt != null) {
                    bt.setText("Not Living");
                }
            }
        }
        return v;
    }

    // provides a way for the Object to call back to the list without
    // blocking the UI
    public class ObjectInterfaceHandler implements ListReadyObjectStatusListener {
        int position;
        ListReadyObject o;
        View v;

        private final Handler handler = new Handler();

        public ObjectInterfaceHandler(int position, ListReadyObject o, View v) {
            this.position = position;
            this.o = o;
            this.v = v;
            // register to observe an update from the Object
            o.registerObserver(this);
        }
        @Override
        public void objectInterfaceUpdate() {
            // called from the Object's observer pattern
            handler.post(updateBottomText);
        }
        // runnable to put the update on the UI Thread
        private Runnable updateBottomText = new Runnable() {
            @Override
            public void run() {
                TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                if (bt != null) {
                    bt.setText(o.getStatusText());
                }
            }
        };
    }
}

ListReadyObject接口:

package com.scs.stuff;

public interface ListReadyObject {

    public void registerObserver(ListReadyObjectStatusListener o);

    public void removeObserver(ListReadyObjectStatusListener o);

    public void notifyObservers();

    public String getDisplayText();

    public String getStatusText();

    public boolean isLiving();

    public void updateStatus();

}

ListReadyObjectStatusListener接口:

package com.scs.stuff;

public interface ListReadyObjectStatusListener {

    public void objectInterfaceUpdate();

}

2 个答案:

答案 0 :(得分:1)

它没有(直接)使视图侦听回调。

在这里,您会看到ListReadyObject传递给ObjectInterfaceHandler的构造函数:

ListReadyObject o = getItem(position);
...
new ObjectInterfaceHandler(position, o, v);

在该构造函数中,您会发现:

o.registerObserver(this);

所以ObjectInterfaceHandler实际上是听ListReadyObject的人。然后,当收到更新通知时:

@Override
public void objectInterfaceUpdate() {
    // called from the Object's observer pattern
    handler.post(updateBottomText);
}

它会向自己的处理程序发布一条消息,以调用更新视图的Runnable

private Runnable updateBottomText = new Runnable() {
    @Override
    public void run() {
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        if (bt != null) {
            bt.setText(o.getStatusText());
        }
    }
};

这是一种非常常见的模式,称为Observer Pattern

答案 1 :(得分:0)

我是Android的新手,我只获得了一半的代码,但我唯一能想到的是ObjectInterfaceHandler的构造函数中的某些东西被调用,这对代码的延续产生了影响。由于您只创建了实例(您没有对它进行任何引用),唯一的区别可能是构造函数的代码:

o.registerObserver(this);

ListReadyObject参数在此处引用ObjectInterfaceHandler。