所以,我从 REST 服务器获取 JSON,我想用它的信息填充 ListView。我所拥有的是:
public class MyReservations extends ListActivity {
....
class HTTPAssync implements Runnable {
public void run () {
... //make the requestions and get a valida JSONObject (that has a JSONArray)
try {
JSONObject jsonObject = new JSONObject(payload);
reservs = jsonObject.getJSONArray("reservs");
try {
listView.setAdapter(new JSONAdapter(MyReservations.this, reservs));
}
我也创建了一个 JSONAdapter:
class JSONAdapter extends BaseAdapter implements ListAdapter {
private final Activity activity;
private final JSONArray jsonArray;
private LayoutInflater mInflater;
public JSONAdapter(Activity activity, JSONArray jsonArray) {
assert activity != null;
assert jsonArray != null;
this.jsonArray = jsonArray;
this.activity = activity;
}
public int getCount() {
return jsonArray.length();
}
public JSONObject getItem(int position) {
return jsonArray.optJSONObject(position);
}
public long getItemId(int position) {
JSONObject jsonObject = getItem(position);
return jsonObject.optLong("id");
}
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = activity.getLayoutInflater().inflate(R.layout.reservation, null);
JSONObject jsonObject = getItem(position);
view = mInflater.inflate(R.layout.reservation, null);
((TextView) view.findViewById(R.id.reservationCourse)).setText("MOTHAFOCKA");
return view;
}
}
但是当我让new Thread(new HTTPAssync()).start();
我得到android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
. 我试图创建一个方法并从中设置适配器,但得到了同样的错误。我该怎么做?