Actually I've been trying to display a user name on Text View when user clicks on the Image View. User name is showing when click on image view but when I scroll down that same name is appearing on different position in List.
For example:
when i click on user profile it shows its name:
But when i scrolls down, that name is repeating on another user.
Here is my code:
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: How to show only one text view. As shown in the pic, I don't want to show two username at a time(two text views) if I click on the other user, only one username at a time.
You should hide it whenever your adapter gets a view. This is caused by ListView Recycling Mechanism.
Therefore, add the following line to your getView
method.
holder.txt_userName.setVisibility(View.GONE);
Note: For better performance, you wouldn't need to register onClick
listener every times. It's enough to register it once. Thus, move that onClick
registration code to if (convertView == null)
block.
UPDATE #1
A simple solution for hiding and re-showing the TextView
. Add the following if
block to your getView
method.
Checkins thisPerson = allPersons.get(position);
if(thisPerson.isUsernameDisplayed()){
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(thisPerson.getUserName());
} else {
holder.txt_userName.setVisibility(View.GONE);
}
You may ask me what isUsernameDisplayed
is. The answer is, you should keep track of each item to see whether its user name is visible or not. For this, you should save their states somewhere. You may want to save this information in the allPersons
itself.