改变适配器数据(Altering Adapter data)

我有一个ListActivity 。 ListView中的每个项目都包含一个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); } }); }

I have a ListActivity. Each item in the ListView contains a RatingBar. When the user manipulates the RatingBar, I want to update the corresponding data attached to the Adapter.

Unfortunately, inside my RatingBar handler, I can't refer to the position variable. I understand why. I'm just having trouble finding a way around it.

How would I handle what I'm trying to do here?

@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); } }); }

最满意答案

我知道您的代码无法正常工作,因为您错误:

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

Here is what I have done...

Inside the onRatingChanged handler, we first need to find the position. I've created a small method to do this:

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; }

The rest is simple. Take the result of this method (x) and call:

m_items.get(x).setRating((int)rating);改变适配器数据(Altering Adapter data)

我有一个ListActivity 。 ListView中的每个项目都包含一个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); } }); }

I have a ListActivity. Each item in the ListView contains a RatingBar. When the user manipulates the RatingBar, I want to update the corresponding data attached to the Adapter.

Unfortunately, inside my RatingBar handler, I can't refer to the position variable. I understand why. I'm just having trouble finding a way around it.

How would I handle what I'm trying to do here?

@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); } }); }

最满意答案

我知道您的代码无法正常工作,因为您错误:

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

Here is what I have done...

Inside the onRatingChanged handler, we first need to find the position. I've created a small method to do this:

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; }

The rest is simple. Take the result of this method (x) and call:

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