I’ve been using this for a while, and I’m not sure if I found this technique in an example or just tried a bunch of things until it finally worked. My question is how does this line of code,
new ObjectInterfaceHandler(position, o, v);
actually make it to where this view Listens for the call backs from the 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 interface:
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 interface:
package com.scs.stuff;
public interface ListReadyObjectStatusListener {
public void objectInterfaceUpdate();
}
It doesn’t (directly) make the view listen for callbacks.
Here you see the
ListReadyObjectget passed into the constructor ofObjectInterfaceHandler:In that constructor you find this:
So the
ObjectInterfaceHandleris actually the one listening toListReadyObject. Then when it is notified of an update:It posts a message to its own handler to call your
Runnablewhich updates the view:This is a very common pattern called the Observer Pattern.