我已经使用了一段时间了,我不确定是在示例中找到了这种技术还是只是尝试了很多方法直到最终奏效。我的问题是这行代码如何
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();
}
最佳答案
它不会(直接)使视图侦听回调。
在这里,您看到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。
关于android - 为什么在自定义ArrayAdapter中实例化新的自定义处理程序有效?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8994064/