Android ListView 项目被重复

标签 android listview android-adapter

我有一个 ListView ,它使用自定义适配器填充信息,显示树状结构。但是,“父级”级别的评论总是重复出现。这是我的自定义适配器的代码:

class CommentListAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;

        public CommentListAdapter(CommentActivity commentActivity){
            layoutInflater = (LayoutInflater) commentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return commentFeed.getCommentCount();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class listViewHolder {
            RelativeLayout spacerRelLayout;
            TextView authorText;
            TextView bodyText;
            TextView timeText;

            listViewHolder(View v) {
                spacerRelLayout = (RelativeLayout) v.findViewById(R.id.spacerLayout);
                authorText = (TextView) v.findViewById(R.id.authorTextComment);
                bodyText = (TextView) v.findViewById(R.id.commentTextView);
                timeText = (TextView) v.findViewById(R.id.timeTextComment);
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View listItem = convertView;
            listViewHolder holder;

            if (listItem == null) {
                listItem = layoutInflater.inflate(R.layout.comment_item_layout, parent, false);
                holder = new listViewHolder(listItem);
                listItem.setTag(holder);
            } else {
                holder = (listViewHolder) listItem.getTag();
            }

            holder.bodyText.setText(Html.fromHtml(commentFeed.getComment(position).getBody()));
            holder.timeText.setText(commentFeed.getComment(position).getTime());
            holder.authorText.setText(commentFeed.getComment(position).getAuthor());
            holder.spacerRelLayout.getLayoutParams().width = commentFeed.getComment(position).getLevel() *10;
            holder.spacerRelLayout.invalidate();

            return listItem;

        }
    }

如何解决这个问题?

最佳答案

getItem 应该是这样的:

@Override
public Object getItem(int position) {
    return commentFeed.getComment(position);
}

然后在您的 getView 中执行此操作:

Comment comment = (Comment) getItem(position);
holder.bodyText.setText(Html.fromHtml(comment.getBody()));
// etc..

关于Android ListView 项目被重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31622480/

相关文章:

android - android中的 ListView 选择颜色

java - 自定义警报对话框内的 ListView 中的空对象引用

java - 适配器值未从其他 Activity 更新

android - 有没有办法将两个 TextView 添加到 arrayadapter

android - 如何更改android gradle ndk -j(--jobs)标志值以进行清理任务?

android - 列表 fragment : prevent race condition between getListView() and onDestroyView()

android - OnDataChange Firebase 安卓监听器

android - 使用Jenkins构建Gradle项目时出错

c# - 从 ListView (WPF) 中的 SelectedItem 启动键盘导航

android - 当我们滚动时,RecyclerView 如何从 Adapter 获取 1 个额外的 View