其实,当用户单击图像视图时,我一直试图在文本视图上显示用户名。点击图片视图时显示用户名,但当我向下滚动时,同名出现在列表中的不同位置。在列表中滚动的TextView重复
例如:
当我点击用户配置文件就说明它的名字:
这里是我的代码:
private List<Checkins> allPersons;
Checkins checkin;
public CustomAdapter(FragmentActivity activity, List<Checkins> classModel)
{
context = activity;
allPersons = classModel;
mActivity = activity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null)
{
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.programlist, null);
holder = new mHolder();
holder.txt_name = (TextView) convertView.findViewById(R.id.fragment_browse_grid_footer_product_name);
holder.txt_userName = (TextView) convertView.findViewById(R.id.txt_userName);
holder.imgV_profile = (ImageView) convertView.findViewById(R.id.imgVProfilePic);
holder.imgV_chceckins = (ImageView) convertView.findViewById(R.id.imageV_CheckinPlace);
holder.imgV_placePic = (ImageView) convertView.findViewById(R.id.checkinPlace_image);
convertView.setTag(holder);
} else{
holder = (mHolder) convertView.getTag();
}
checkin = allPersons.get(position);
holder.txt_name.setText(checkin.getPlaceName());
holder.imgV_profile.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Checkins checkin2 = allPersons.get(position);
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(checkin2.getUserName());
}
});
return convertView;
}
class mHolder {
TextView txt_name;
TextView txt_userName;
ImageView imgV_profile;
ImageView imgV_chceckins;
ImageView imgV_placePic;
int pos;
}
UPDATE:如何只显示一个文本视图。 如图所示,我不想一次显示两个用户名(两个文本视图),如果我点击其他用户,一次只有一个用户名。
而不是在'onClick'事件中再次执行'Checkins checkin2 = allPersons.get(position);',为什么不直接声明'checkin'为final并在'onClick'中使用?另外,为什么你必须在onClick中设置'setText()'?你不能只在getView本身设置文本? – Bhargav