Listview没有更新搜索

时间:2013-06-18 08:22:02

标签: android android-listview android-xml

我正在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 个答案:

答案 0 :(得分:1)

您在哪里使用啤酒列表在列表视图中显示它?

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

答案 1 :(得分:1)

使用适配器设置tha数组

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

      lv.setAdapter(arrayAdapter);

答案 2 :(得分: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

答案 3 :(得分: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,将数据传递到适配器

来创建自定义适配器