如何更改/更新列表视图的单个列表项?

时间:2016-04-29 15:34:03

标签: android listview android-arrayadapter

我正在尝试(大约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方法,但没有任何反应。

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

感谢。

1 个答案:

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

}