改变适配器数据

时间:2010-08-11 20:05:19

标签: android listview listactivity

我有ListActivityListView中的每个项目都包含RatingBar。当用户操作RatingBar时,我想更新附加到适配器的相应数据。

不幸的是,在我的RatingBar处理程序中,我无法引用位置变量。我理解为什么。我只是找不到解决方法。

我如何处理我在这里要做的事情?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // grab view
    View v = convertView;
    // set view layout
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.layout_rating_ratingbubble, null);
        RatingBar r = (RatingBar)v.findViewById(R.id.rating_rating);
        if (r != null) {
            r.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                    public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromUser) {
                        // CANT REFER TO position HERE
                        m_items.get(position).setRating((int)rating);
                    }
                });
        }

3 个答案:

答案 0 :(得分:0)

我了解您的代码无法正常处理错误:

m_items.get(position).setRating((int)rating);

尝试这样做:

if (r != null) {
            final int position2 = position;
            r.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                    public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromUser) {
                        m_items.get(position2).setRating((int)rating);
                    }
                });

答案 1 :(得分:0)

这样的事情怎么样:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // grab view
    View v = convertView;
    // set view layout
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.layout_rating_ratingbubble, null);
        RatingBar r = (RatingBar)v.findViewById(R.id.rating_rating);
        if (r != null) {
            r.setTag(position); // Set the position number as the tag on the view
            r.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                    public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromUser) {
                        int item_position = (int)ratingBar.getTag(); // Retrieve tag
                        m_items.get(item_position).setRating((int)rating);
                    }
                });
        }
        return v;
}

虽然,我可能会将RatingBar.OnRatingBarChangeListener接口添加到另一个实例化的对象中。 (即使是适配器,如果你愿意,虽然它可能不属于那里。)实际上,你正在为每个RatingBar创建一个新的监听器对象,这在内存部门有点浪费。

答案 2 :(得分:0)

这就是我所做的......

在onRatingChanged处理程序中,我们首先需要找到位置。我已经创建了一个小方法来执行此操作:

    private int getListPosition(View v) {
        View vParent = (View)v.getParent();
        if (vParent != null) {
            ViewGroup vg = (ViewGroup)vParent.getParent();
            if (vg != null) {
                return getListView().getFirstVisiblePosition() + vg.indexOfChild(vParent);
            }
        }
        return -1;
    }

其余的很简单。取这个方法的结果(x)并调用:

m_items.get(x).setRating((int)rating);