带有viewholder的Android Listview正在返回重复的条目

时间:2014-11-05 02:38:53

标签: android listview android-listview

我有一个列表视图,它有两种不同的布局类型。

1:分享 2:不共享

很简单。

我正在使用视图来处理回收,但由于某种原因,我仍然在列表视图中获得重复的条目。

BIG注意:我不想根据列表视图中项目的位置更改布局,因此我不认为使用getItemViewType会有所帮助,然后我可能会错。

我正在根据从数据库传回的布尔值来更改项目布局。

以下是代码:

@Override
public View getItemView(final StatusItem status, View convertView, ViewGroup parent) {

    View view = convertView;

    // we'll set up the ViewHolder
    ViewHolder viewHolder;

    shared = status.getShared();

    if (view == null) {
        // If view is null
        viewHolder = new ViewHolder();

        if (shared) {
            // if the status is shared use this layout
            view = mInflater.inflate(R.layout.status_list_view_item_shared, parent, false);
            viewHolder.OUserName = (TextView) view.findViewById(R.id.OUserName);
            viewHolder.OUserName.setText("@" + status.getOUN());

            viewHolder.sharedText= (TextView) view.findViewById(R.id.sharedText);
            viewHolder.sharedText.setText(status.getSharedStatus());
        } else {
            // if the status is not shared use this layout
            view = mInflater.inflate(R.layout.status_list_view_item, parent, false);
        }

        mListViewReferences(view, status, viewHolder);
        loadProfileImages(status, viewHolder);
        view.setTag(viewHolder);
    } else {
        // we've just avoided calling findViewById() on resource everytime
        viewHolder = (ViewHolder) view.getTag();
    }

这是我的viewHolder

private static class ViewHolder {
    TextView statusUserName, timePosted, OUserName, sharedText, commentCounter, likeCounter, statusText;
    ImageButton statusLike, statusComments, statusShare, statusMenu;
    ImageView statusProfilePic;
    LinearLayout likeCounterLayout, commentCounterLayout;

}

1 个答案:

答案 0 :(得分:1)

试试这个兄弟或先生?

 @Override
public View getItemView(final StatusItem status, View convertView, ViewGroup parent) {

View view = convertView;

// we'll set up the ViewHolder
ViewHolder viewHolder;

shared = status.getShared();

if (view == null) {
    // If view is null
    viewHolder = new ViewHolder();

    if (shared) {
        // if the status is shared use this layout
        view = mInflater.inflate(R.layout.status_list_view_item_shared, parent, false);
        viewHolder.OUserName = (TextView) view.findViewById(R.id.OUserName);
        viewHolder.OUserName.setText("@" + status.getOUN());

        viewHolder.sharedText= (TextView) view.findViewById(R.id.sharedText);
        viewHolder.sharedText.setText(status.getSharedStatus());
    } else {
        // if the status is not shared use this layout
        view = mInflater.inflate(R.layout.status_list_view_item, parent, false);
    }        
} else {
    // we've just avoided calling findViewById() on resource everytime
    viewHolder = (ViewHolder) view.getTag();
}

 mListViewReferences(view, status, viewHolder);
 loadProfileImages(status, viewHolder);
 view.setTag(viewHolder);
}

复制,粘贴并尝试..如果条件及其在getview方法中,请确保它们在视图之外为null