其實,當用戶單擊圖像視圖時,我一直試圖在文本視圖上顯示用戶名。點擊圖片視圖時顯示用戶名,但當我向下滾動時,同名出現在列表中的不同位置。在列表中滾動的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