我一直在尝试实现listview实现,其中显示textview或根据使用BaseAdapter在活动中输入到ArrayList的数据显示imageview。
我对自定义基础适配器的实现:
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import com.android.pls.R;
import com.android.pls.common.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* @author ANKIT
*
*/
public class CommentSplashListViewAdapter extends BaseAdapter
{
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
private List<CommentSplashList> comments = new ArrayList<CommentSplashList>();
private ArrayList<CommentSplashList> arraylist;
public CommentSplashListViewAdapter(Context context, List<CommentSplashList> comments)
{
this.context = context;
this.comments = comments;
this.arraylist = new ArrayList<CommentSplashList>();
imageLoader = new ImageLoader(context);
}
public void setListItems(List<CommentSplashList> commentLists)
{
this.comments = commentLists;
this.arraylist.addAll(commentLists);
}
@Override
public int getCount()
{
return comments.size();
}
@Override
public Object getItem(int position)
{
return comments.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public int getViewTypeCount()
{
return 2;
}
@Override
public int getItemViewType(int position)
{
CommentSplashList comment = comments.get(position);
if(comment.getContainsUrl() == false)
return 0;
else
return 1;
}
public static class WithoutSplashbackViewHolder
{
TextView creatorUserName;
ImageView creatorProfilePic;
TextView userComment;
TextView timeAgo;
//ImageView splashbackImage;
}
public static class WithSplashbackViewHolder
{
TextView creatorUserName;
ImageView creatorProfilePic;
//TextView userComment;
TextView timeAgo;
ImageView splashbackImage;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
CommentSplashList comment = comments.get(position);
WithoutSplashbackViewHolder holder1 = null;
WithSplashbackViewHolder holder2 = null;
View v = convertView;
if (getItemViewType(position) == 0)
{
holder1 = new WithoutSplashbackViewHolder();
if (v == null)
{
//GET View 1
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.splashitem_comment_without_splashback, null);
v = viewGroup;
holder1.creatorProfilePic = (ImageView) v.findViewById(R.id.creatorProfilePic);
holder1.creatorUserName = (TextView) v.findViewById(R.id.creatorUserName);
holder1.timeAgo = (TextView) v.findViewById(R.id.commentTimeAgo);
holder1.userComment = (TextView) v.findViewById(R.id.userComment);
//holder.splashbackImage = (ImageView) v.findViewById(R.id.splashbackImage);
}
// Set the results into TextViews
holder1.creatorUserName.setText(comment.getCreatorUserName());
holder1.timeAgo.setText(comment.getTimeAgo());
// Set the results into ImageView
if(comment.getCreatorProfilePic() != "null")
imageLoader.DisplayImage(comment.getCreatorProfilePic(),
holder1.creatorProfilePic);
holder1.userComment.setText(comment.getUserComment());
return v;
}
else
{
holder2 = new WithSplashbackViewHolder();
if (v == null)
{
//GET View 2
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.splashitem_comment_with_splashback, null);
v = viewGroup;
holder2.creatorProfilePic = (ImageView) v.findViewById(R.id.creatorProfilePic);
holder2.creatorUserName = (TextView) v.findViewById(R.id.creatorUserName);
holder2.timeAgo = (TextView) v.findViewById(R.id.commentTimeAgo);
//holder.userComment = (TextView) v.findViewById(R.id.userComment);
holder2.splashbackImage = (ImageView) v.findViewById(R.id.splashbackImage);
}
// Set the results into TextViews
holder2.creatorUserName.setText(comment.getCreatorUserName());
//holder2.creatorUserName.setText("username");
holder2.timeAgo.setText(comment.getTimeAgo());
// Set the results into ImageView
if(comment.getCreatorProfilePic() != "null")
imageLoader.DisplayImage(comment.getCreatorProfilePic(),
holder2.creatorProfilePic);
imageLoader.DisplayImage(comment.getSplashbackImage(),
holder2.splashbackImage);
return v;
}
}
}
我还试图删除这些行:if(v == null)及其循环,它删除了错误,但它没有用toplashbackviewholder对视图进行膨胀。
我在堆栈和在线搜索但我找到了解决方案,他们只有多个布局,只有textview而不是imageviews。
答案 0 :(得分:0)
我猜你误解了View Holders的含义。 您应该将ViewHolder添加为convertView的标记。 一般逻辑如下:
if (converView == null) {
convertView = inflateView();
holder = new Holder(convertView);
converView.setTag(holder);
} else {
holder = (Holder)convertView.getTag();
}
另外,如果getItemViewTypeCount()= 2,那么你必须有2种项目类型,而不是3种(你有0,1,2)。
请告诉我这个方法:
comment.getContainsUrl()