我正在 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)