我想用两个不同的布局使用ArrayAdapter充气listView。Android如何充气ListView与两个不同的项目
我想显示一些用户帖子。如果它包含图片,则使用一个布局,如果它不使用第二个。
我这样做,到目前为止
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Post post = getItem(position);
boolean noImg = post.mImgUrl1 == null && post.mImgUrl2 == null;
if (convertView == null){
if (noImg)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item_no_img, parent ,false);
else
convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item, parent, false);
}
if (noImg == false) {
mTitle = (TextView) convertView.findViewById(R.id.tv_titlePost);
mText = (TextView) convertView.findViewById(R.id.tv_textPost);
mImage = (ImageView) convertView.findViewById(R.id.iv_postimg);
}
else {
mNoImgTitle = (TextView) convertView.findViewById(R.id.tv_noImgTitle);
mNoImgText = (TextView) convertView.findViewById(R.id.tv_noImgText);
}
if (noImg) {
mNoImgTitle.setText(post.mTitle);
mNoImgText.setText(post.mText);
if (post.mImgUrl1 != null)
Glide.with(mImage.getContext()).load(post.mImgUrl1).into(mImage);
else Glide.with(mImage.getContext()).load(post.mImgUrl2).into(mImage);
}
else {
mNoImgTitle.setText(post.mTitle);
mNoImgText.setText(post.mText);
}
return convertView;
}
但我意识到,从第二布局textviews无法通过findViewById功能被发现。它总是返回一个空指针。
我的解决方案是什么?我应该只做一个布局,并以某种方式使用可视性来欺骗它?或者有一种方法?
的可能的复制[Android的列表视图ArrayAdapter与两个布局](https://stackoverflow.com/questions/29721753/android-listview-arrayadapter-with-two-layouts) –