2016-04-29 240 views
0

我正在尝试(约4天)来更改特定列表视图项目的视图。我尝试了很多可用的解决方案,虽然这个问题看起来可能是“重复的”,但我已尽最大努力实施所有解决方案,但仍然没有运气。如何更改/更新列表视图的单个列表项?

所以这里是我的问题,我有一个列表视图,它是通过使用延伸BaseAdapter的定制适配器创建的。

现在需要发生的是,根据状态,特定列表项目中的图像将被改变。

下面是一些代码:根据状态操纵

nowPlayingListView = (ListView) findViewById(R.id.nowPlayingListView); 
    nowPlayingListViewAdapter = new NowPlayingListViewAdapter(this, songsList); 
    nowPlayingListView.setAdapter(nowPlayingListViewAdapter); 

这里:

public void handleSelectedState(Drawable dr) { 
    int currentPosition=getPosition(); 
    View singLeListItem = getViewByPosition(currentPosition, nowPlayingListView); 

    ImageButton imgButton = (ImageButton) findViewById(R.id.npListViewSingleImageButton); 

    int count = ((ViewGroup) singLeListItem).getChildCount(); 

    for (int i = 0; i < count; i++) { 
     View nextChild = ((ViewGroup) singLeListItem).getChildAt(i); 
     try { 
      //Log.d("Loop", count + "" + nextChild.getTag() + nextChild.getTag().equals("linearLayoutImageButton")); 
      if (nextChild.getTag().equals(getResources().getString(R.string.NPSingleItemLinearLayoutImgButton))) { 
       imgButton = (ImageButton) ((ViewGroup) nextChild).getChildAt(0); 
       imgButton.setImageDrawable(null); 
       imgButton.setImageDrawable(dr); 

       break; 
      } 
     } catch (Exception e) { 

     } 
    } 
} 

public View getViewByPosition(int pos, ListView listView) { 
    final int firstListItemPosition = listView.getFirstVisiblePosition(); 
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; 

    if (pos < firstListItemPosition || pos > lastListItemPosition) { 

     return listView.getAdapter().getView(pos, null, listView); 
    } else { 
     final int childIndex = pos - firstListItemPosition; 
     return listView.getChildAt(childIndex); 
    } 
} 

我试图做adpater.notifydatasetchanged方法图像绘制被设定后

列表视图初始化但没有任何反应。

此外,代码仅适用于可见的列表项,但这不是我所需要的。我想更新不可见的列表项目的更改。

感谢。

+0

这是最好的改变你的适配器来处理这些状态 – Pooya

+0

你究竟是什么意思? –

回答

0

您的自定义适配器的实现应该是这样的,您可以在getView()方法中对视图进行相关更改。你的实现不是这样做的正确方法。

private class RestaurantArrayAdapter extends ArrayAdapter<Restaurant> { 

    private Context context; 
    private List<Restaurant> restaurantList; 

    public RestaurantArrayAdapter(Context context, int textViewResourceId, 
            List<Restaurant> restaurantList) { 
     super(context, textViewResourceId, restaurantList); 
     this.context = context; 
     this.restaurantList = restaurantList; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.row_layout, parent, false); 
     TextView restaurantNameTextView = (TextView) rowView.findViewById(R.id.restaurantNameTextView); 
     TextView addressTextView = (TextView) rowView.findViewById(R.id.addressTextView); 
     TextView typeTextView = (TextView) rowView.findViewById(R.id.typeTextView); 
     RatingBar ratingBar = (RatingBar) rowView.findViewById(R.id.ratingBar); 
     restaurantNameTextView.setText(restaurantList.get(position).getName()); 
     addressTextView.setText(restaurantList.get(position).getAddress()); 
     typeTextView.setText(restaurantList.get(position).getType()); 
     float rating = (float) restaurantList.get(position).getRating(); 
     ratingBar.setRating(rating); 

     return rowView; 
    } 

} 
+0

你的适配器实现和我的没有区别,除了你使用阵列适配器和我使用基本适配器 –

+0

我不认为你应该明确调用getView()方法。你可能想要检查这个实现http://www.piwai.info/android-adapter-good-practices/ – Anurag

+0

我已经阅读过它,但它包含很多信息,你能在那里解释“我的部分”吗? –