Android で最初のリスト ビューを作成しています。APIのクエリに成功し、結果を取得して独自のオブジェクトに入れ、各オブジェクトを配列リストに追加しました。すべて正常に動作していますが、リストが更新されていません。
これは、返された json からデータを取得し、項目をリストに追加してから、リスト ビューを更新しようとする場所です。
//get json items
for(int i = 0; i < json.getJSONArray("data").length(); i++) {
String beerId = GetBeerDataFromJSON(i,"id", json);
String beerName = GetBeerDataFromJSON(i,"name", json);
String beerDescription = GetBeerDataFromJSON(i,"description" , json);
String beerAbv = GetBeerDataFromJSON(i,"abv" , json);
String beerIbu = GetBeerDataFromJSON(i,"ibu" , json);
String beerIcon = GetBeerIconsFromJSON(i, "icon",json );
String beerMediumIcon = GetBeerIconsFromJSON(i, "medium",json );
String beerGlass = GetBeerGlassFromJSON(i, json );
String beerStyle = GetBeerStyleFromJSON(i,"name", json );
String beerStyleDescription = GetBeerStyleFromJSON(i,"description", json );
String beerBreweryId = GetBeerBreweryInfoFromJSON(i, "id", json );
String beerBreweryName = GetBeerBreweryInfoFromJSON(i, "name", json );
String beerBreweryDescription = GetBeerBreweryInfoFromJSON(i, "description", json );
String beerBreweryWebsite = GetBeerBreweryInfoFromJSON(i, "website", json );
String beerBreweryYear = GetBeerBreweryInfoFromJSON(i, "established", json );
//create beer object
BeerData thisBeer = new BeerData(beerName, beerId, beerDescription, beerAbv, beerIbu, beerIcon,
beerMediumIcon, beerGlass, beerStyle, beerStyleDescription, beerBreweryId, beerBreweryName,
beerBreweryDescription, beerBreweryYear, beerBreweryWebsite);
//add beer to list
beerList.add(thisBeer);
}
//update listview
ArrayAdapter<BeerData> arrayAdapter = new ArrayAdapter<BeerData>(MainActivity.this,R.layout.list_item);
lv.setAdapter(arrayAdapter);
私のlist_item xmlは次のとおりです。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
私のmain_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" >
</ListView>
</RelativeLayout>
アップデート
以下の回答のおかげで、アダプターを介してデータリストを渡していないことに気付きました。これを行うと、次のエラーが発生します。
06-18 09:34:37.631: E/AndroidRuntime(13761): FATAL EXCEPTION: main
06-18 09:34:37.631: E/AndroidRuntime(13761): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.ListView.onMeasure(ListView.java:1158)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.View.measure(View.java:15525)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.View.measure(View.java:15525)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.View.measure(View.java:15525)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.View.measure(View.java:15525)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-18 09:34:37.631: E/AndroidRuntime(13761): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2434)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.View.measure(View.java:15525)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.os.Handler.handleCallback(Handler.java:725)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.os.Looper.loop(Looper.java:137)
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.app.ActivityThread.main(ActivityThread.java:5226)
06-18 09:34:37.631: E/AndroidRuntime(13761): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 09:34:37.631: E/AndroidRuntime(13761): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 09:34:37.631: E/AndroidRuntime(13761): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-18 09:34:37.631: E/AndroidRuntime(13761): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-18 09:34:37.631: E/AndroidRuntime(13761): at dalvik.system.NativeStart.main(Native Method)
06-18 09:34:37.631: E/AndroidRuntime(13761): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
06-18 09:34:37.631: E/AndroidRuntime(13761): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)