【问题标题】:Adding RatingBar to ListAdapter using Simple Adapter not working Correctly使用简单适配器将 RatingBar 添加到 ListAdapter 无法正常工作
【发布时间】:2012-10-14 02:08:05
【问题描述】:

我的 android 应用程序中有一个 ListAdapter...我正在尝试将 ratingBar 添加到列表中的每个项目,并将其值设置为 [TAG_RATING] 中包含的值。问题是列表正在显示并且唯一的 ratingBar 卡在列表的最底部并且相当大。

请看下面的代码:

   ListAdapter adapter = new SimpleAdapter(
                        AllProductsActivity.this, productList,
                        R.layout.list_item, new String[] { TAG_BID,
                                TAG_NAME,
                                TAG_RATING},
                        new int[] { R.id.pid, R.id.name, R.id.ratingBar});
                    ((SimpleAdapter) adapter).setViewBinder(new MyBinder());
                // updating listview
                setListAdapter(adapter);

那么 ViewBinder 类看起来像这样:

   class MyBinder implements ViewBinder{
    public boolean setViewValue(View view, Object data, String textRepresentation) {
        if(view.getId() == R.id.ratingBar){
            String stringval = (String) data;
            float ratingValue = Float.parseFloat(stringval);
            RatingBar ratingBar = (RatingBar) view;
            ratingBar.setRating(ratingValue);
            return true;
        }
        return false;
    }
}

列表项xml文件如下所示:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >


<TextView
    android:id="@+id/pid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<!-- Name Label -->
<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="17dip"
    android:textStyle="bold" />

  <RatingBar
    android:id="@+id/rating"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:stepSize="0.25"
    android:numStars="5"
    />

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    RatingBar 小部件应放置在您在适配器中使用的列表行布局中,R.layout.list_item。现在,您只是在主布局中的ListView 下方放置了一个RatingBar 小部件。

    【讨论】:

    • @Skindeep2366 您应该发布行布局文件。如果删除isIndicator 属性,RatingBar 会变小吗?
    • @Skindeep2366 将RatingBar 的宽度设置为wrap_content 以仅查看这5 颗星。然后您可以使用?android:attr/ratingBarStyleSmall 使其更小(添加style="?android:attr/ratingBarStyleSmall")。
    • @Skindeep2366 好吧,它有用吗?您也可以使用?android:attr/ratingBarStyleIndicator 作为样式。小部件不是很完美,例如看这个问题:stackoverflow.com/questions/2874537/…
    • @Skindeep2366 如果您希望它们位于同一行,则将LinearLayout 的方向设置为horizontal,将名称 TextView 的宽度设置为@987654336 @。当然,这会将 TextView 放在同一行,但我不知道你想用它做什么。还要确保TextViewRatingBar 在一条线上有空间。
    • @Skindeep2366 使用相同的ViewBinder,如果视图的ID 是R.id.name,则从data 参数中减去一个最多20 个字符长的子字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多