安卓:你怎么能设置一个ListAdapter内的RatingBar的价值? [英] Android: How can you set the value of a ratingBar within a ListAdapter?

查看:213
本文介绍了安卓:你怎么能设置一个ListAdapter内的RatingBar的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何设置的RatingBar是在列表中的值,但一直没能弄明白。

I'm trying to figure out how to set the value of a ratingBar that is in a list, but haven't been able to figure it out.

我目前使用一个简单的适配器设置文本。

I'm currently using a simple adapter to set the text.

其中的 MYLIST 的是一个HashMap。

Where mylist is a hashmap.

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_simple, 
                            new String[] { "name"}, 
                            new int[] { R.id.item_title});

setListAdapter(adapter);

和我的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@color/white">


    <TextView  
    style="@style/ListHeading"
    android:id="@+id/item_title"
    android:gravity="left"/>

    <RatingBar 
    style="@style/RatingBarSm" 
    android:id="@+id/ratingBar" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:isIndicator="true"/>         

</LinearLayout>

当我尝试添加评级来简单的适配器,我得到这个错误。

When I try to add "rating" to the simple adapter, I get this error.

ERROR/AndroidRuntime(10793): java.lang.IllegalStateException: android.widget.RatingBar is not a  view that can be bounds by this SimpleAdapter

我一直没能找到任何关于这个有用。任何帮助或方向大大AP preciated。

I haven't been able to find anything useful on this. Any help or direction is greatly appreciated.

推荐答案

您可以使用SimpleAdapter.ViewBinder作为活动中的私有类

You can use SimpleAdapter.ViewBinder as a private class within the activity

class MyBinder implements ViewBinder{
    @Override
    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;
    }
}

然后可以以下列方式的ViewBinder设置为SimpleAdapter

Then you can set the ViewBinder to the SimpleAdapter in the following manner.

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_simple, 
new String[] { "name", "rating"}, 
new int[] { R.id.item_title, R.id.ratingBar});
adapter.setViewBinder(new MyBinder());
setListAdapter(adapter);

将数据绑定到的意见分两个阶段进行。首先,如果 SimpleAdapter.ViewBinder 可用, setViewValue(android.view.View,对象,字符串)被调用。如果返回值是true,结合已经发生。如果返回值是假的,意见,然后由SimpleAdapter按顺序尝试。

Binding data to views occurs in two phases. First, if a SimpleAdapter.ViewBinder is available, setViewValue(android.view.View, Object, String) is invoked. If the returned value is true, binding has occurred. If the returned value is false, the views are then tried by SimpleAdapter in order.

有关更多的相关信息,您可以浏览 SimpleAdapter

For more info on this you can browse the SimpleAdapter

我想我们没有必要创建自定义列表适配器。

I guess we need not create a custom list adapter.

这篇关于安卓:你怎么能设置一个ListAdapter内的RatingBar的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆