0

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

4 回答 4

1

你在哪里使用 beerlist 在列表视图中显示它?

为数组适配器尝试不同的构造函数,并选择将列表放在构造函数中。例如:像这样 ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,R.layout.list_item, beerList);

于 2013-06-18T08:29:36.657 回答
1

我已经看到了与上述问题相关的上一个问题。

您的课程应该扩展Activity.

你的 listitem.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>

适当地使用 ArrayAdapter 构造函数。

ArrayAdapter<BeerData> adapter1 = new ArrayAdapter<BeerData>(MainActivity.this, R.layout.listitem, R.id.itemname, beerlist);

同样在您的评论中,您说您未定义 arrayadapter 构造函数。这是因为您使用的是this. 使用活动上下文,例如MainActivity.this.

有关更多信息,请查看以下链接

http://developer.android.com/reference/android/widget/ArrayAdapter.html

于 2013-06-18T10:08:25.157 回答
1

使用适配器设置 tha 数组

ArrayAdapter<BeerData> arrayAdapter = new ArrayAdapter<BeerData>(MainActivity.this,R.layout.list_item, beerList);

      lv.setAdapter(arrayAdapter);
于 2013-06-18T08:31:47.283 回答
0

您需要在字符串数组中传递列表数据。

//Fill this array with strings that can be shown in the list
String[] arrStrBeerData;

ArrayAdapter<BeerData> alAdapter = new ArrayAdapter<BeerData>(context, textViewResourceId, arrStrBeerData);
lv.setAdapter(arrayAdapter);

或者

您可以通过扩展 BaseAdapter 来创建自定义适配器,将数据传递给适配器

于 2013-06-18T08:28:41.017 回答