我创建了一个适配器,并尝试使用String ArrayList中的数据填充ListView。我注意到的是,我的ListView中存在的每个第4,第8和第12项都不存在于我的ListView中。如果我删除if / else(view == null)条件,除了收到lint警告>>之外,一切都很完美。 Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling
这是我的getView代码,每跳过第4,第8,第12项
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_select, null);
holder = new ViewHolder();
holder.rlLeft = (RelativeLayout) view.findViewById(R.id.rl_left);
holder.rlRight = (RelativeLayout) view.findViewById(R.id.rl_right);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_title);
holder.tvLeftBubble = (TextView) view.findViewById(R.id.tv_left_bubble);
holder.tvRightBubble = (TextView) view.findViewById(R.id.tv_right_bubble);
holder.tvTitle.setText(mArrySelect.get(position).getTitle());
// set visibility gone on views
if (position % 2 == 0) {
holder.rlLeft.setVisibility(View.GONE);
holder.tvRightBubble.setText(mArrySelect.get(position).getMessage());
} else {
holder.rlRight.setVisibility(View.GONE);
holder.tvLeftBubble.setText(mArrySelect.get(position).getMessage());
}
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view;
}
以下代码是WORKS并显示我的mArraySelect(String ArrayList)中的所有项目,但在我的inflate上给了我一个lint警告
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_select, null);
holder = new ViewHolder();
holder.rlLeft = (RelativeLayout) view.findViewById(R.id.rl_left);
holder.rlRight = (RelativeLayout) view.findViewById(R.id.rl_right);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_title);
holder.tvLeftBubble = (TextView) view.findViewById(R.id.tv_left_bubble);
holder.tvRightBubble = (TextView) view.findViewById(R.id.tv_right_bubble);
holder.tvTitle.setText(mArrySelect.get(position).getTitle());
// set visibility gone on views
if (position % 2 == 0) {
holder.rlLeft.setVisibility(View.GONE);
holder.tvRightBubble.setText(mArrySelect.get(position).getMessage());
} else {
holder.rlRight.setVisibility(View.GONE);
holder.tvLeftBubble.setText(mArrySelect.get(position).getMessage());
}
}
任何人都知道我在第一次实现中缺少什么导致跳过视图?我的mod条件只是交替两个不同的视图,并且工作正常。只是我的String ArrayList中的第4个,第8个和第12个项目没有显示。该阵列只有12个项目。
提前谢谢!
答案 0 :(得分:1)
将以下代码移出最外面的if块可以解决此问题 -
// set visibility gone on views
if (position % 2 == 0) {
holder.rlLeft.setVisibility(View.GONE);
holder.tvRightBubble.setText(mArrySelect.get(position).getMessage());
} else {
holder.rlRight.setVisibility(View.GONE);
holder.tvLeftBubble.setText(mArrySelect.get(position).getMessage());
}
将它放在return语句的上方。