-1

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:

enter image description here

But when i scrolls down, that name is repeating on another user. enter image description here

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.

enter image description here

Mustafa Adil
  • 85
  • 1
  • 10
  • instead of doing `Checkins checkin2 = allPersons.get(position);` again in the `onClick` event, why not just declare `checkin` as final and use that in the `onClick` ? Also why do you have to `setText()` in the onClick? can't you just set text in the getView itself? – Bhargav Nov 05 '15 at 14:17

3 Answers3

2

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.

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • where to place this line in getView method cause thats not what I actually want. I want to stop repeating textview – Mustafa Adil Nov 05 '15 at 14:39
  • @MustafaAdil Its place actually doesn't matter. For example, before `return convertView;`. – frogatto Nov 05 '15 at 14:57
  • ok it works now when i scroll down but when I scroll up the text view on that user is gone. – Mustafa Adil Nov 05 '15 at 15:03
  • can you kindly tell me how should I re-show that specific user name again since its visibility is gone now. – Mustafa Adil Nov 05 '15 at 18:17
  • its working now but I'm having a hard time to set the visibility of only one text view, how to hide the text view before showing other text view?? – Mustafa Adil Nov 08 '15 at 09:59
  • @MustafaAdil What's your mean by _"... I'm having a hard time to set the visibility of only one text view ..."_ ? I couldn't understand what you said. – frogatto Nov 08 '15 at 12:03
0

remove this checkin = allPersons.get(position);

use your allPersons List like below

holder.txt_userName.setVisibility(View.VISIBLE);
            holder.txt_userName.setText(allPersons.get(position).getUserName());
Kastriot Dreshaj
  • 1,121
  • 6
  • 16
0

Use a list to store the position that show nickname!

And set the TextView'Visibility according to the list.

It because reusing ViewHolder!

   private List<Checkins> allPersons; 
    private SparseArray<Integer> list = new SparseArray<>();
    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(); 
    }  

    if(list.get(position)!=null &&list.get(position) == true){
      holder.txt_userName.setVisibility(View.VISIBLE); 
    }else{
      holder.txt_userName.setVisibility(View.GONE); 
   }

    checkin = allPersons.get(position); 
    holder.txt_name.setText(checkin.getPlaceName()); 
    holder.imgV_profile.setOnClickListener(new View.OnClickListener() 
    { 

        @Override 
        public void onClick(View v) 
        { 
            list.append(position,true);
            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;
} 

if you need only one ,you even needn't list.Just use a int to store it.

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • I changed the data type of Sparse Array to boolean otherwise it was showing an error on list.append(position,true). I've also tried this code, but now if I click on other user it shows its user name without hiding the previous one. – Mustafa Adil Nov 05 '15 at 18:14