2010-08-11 87 views
1

我有一個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); 
        } 
       }); 
     } 
+0

任何人有任何其他想法? – Andrew 2010-08-12 14:11:29

回答

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); 
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); 
        } 
       }); 
+0

感謝您的回覆。這對我不起作用。當我調試處理程序時,它說position2無法解析。 – Andrew 2010-08-11 21:21:43

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創建一個新的偵聽器對象,並且在內存部門有點浪費。

+0

有人發佈此解決方案,然後刪除解決方案,一旦我提出了我的問題。我的問題是我的列表可以被用戶改變(例如:刪除一行)。這會混淆刪除行下面的行的所有索引。也許在這個IF語句中添加ELSE case並更新標籤會有所幫助,但顯然只有當行進入視圖時纔會發生。有可能會導致奇怪的行爲。 – Andrew 2010-08-13 13:52:22

+0

我認爲標記實際上只在使用行的ID時纔有用,因爲它與它後面的數據有關。使用索引變得有問題。 – Andrew 2010-08-13 13:56:43