そのため、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.
. メソッドを作成し、そこからアダプターを設定しようとしましたが、同じエラーが発生しました。どうすればいいですか?