Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8205783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:23:45+00:00 2026-06-07T08:23:45+00:00

I have a ListView which extends BaseAdapter . I have a data[] array. The

  • 0

I have a ListView which extends BaseAdapter. I have a data[] array. The ListView inflates and populates correctly. What I am trying to do is make a ImageView visible on the list item (basically a check image on the right side of the inflated view) when the user selects the item and if there was a previous item selected, I just hide that ImageView. This works fine too.

But after I select a new item and scroll back and forth I see weird behavior, the check image is sometimes visible in multiple list items or is hidden in the actual item that is currently selected. Could someone please help and explain what I am doing wrong?

I have these two lines in the onCreate method:

    adap = new EfficientAdapter(this);
    lstview.setAdapter(adap);

and the adapter code:

public static class EfficientAdapter extends BaseAdapter implements Filterable {
  private LayoutInflater mInflater;
  private Context context;
  private ImageView CurrentSelectedImageView;
  private Integer CurrentPosition = 14;


  public EfficientAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
    this.context = context;
  }


  public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    //Log.e("TAG",String.valueOf(position));

    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.adaptor_content, null);

      holder = new ViewHolder();
      holder.textLine = (TextView) convertView.findViewById(R.id.txtCategoryCaption);
      holder.iconLine = (ImageView) convertView.findViewById(R.id.iconLine);
      holder.imgCheckbox = (ImageView) convertView.findViewById(R.id.imgCheck);


      //If the CurrentPosition == position then make the checkbox visible else dont.
      if (CurrentPosition == position){

        holder.imgCheckbox.setVisibility(View.VISIBLE);
      }else{
        holder.imgCheckbox.setVisibility(View.INVISIBLE);
      }


      final ImageView Checkbox = holder.imgCheckbox;

      //Now if the list item is clicked then set the position as the current item and make the checkbox visible.

      convertView.setOnClickListener(new OnClickListener() {
        private int pos = position;

        @Override
        public void onClick(View v) {
            if (CurrentSelectedImageView!=null){
                CurrentSelectedImageView.setVisibility(View.INVISIBLE);
            }
            Checkbox.setVisibility(View.VISIBLE);
            CurrentSelectedImageView = Checkbox;
            CurrentPosition = pos;

        }
      });

      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }


        int id = context.getResources().getIdentifier("nodeinsert", "drawable", context.getString(R.string.package_str));
        if (id != 0x0) {
          mIcon1 = BitmapFactory.decodeResource(context.getResources(), id);
        }

        holder.iconLine.setImageBitmap(mIcon1);
        holder.textLine.setText(String.valueOf(data[position]));


    if (CurrentPosition == position){
        Log.e("TAG",CurrentPosition + "---" + String.valueOf(position));
        holder.imgCheckbox.setVisibility(View.VISIBLE);
    }else{
        holder.imgCheckbox.setVisibility(View.INVISIBLE);
    }


    return convertView;
  }



  static class ViewHolder {
    TextView textLine;
    ImageView iconLine;
    ImageView imgCheckbox;
  }

  @Override
  public Filter getFilter() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return data.length;
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return data[position];
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:23:48+00:00Added an answer on June 7, 2026 at 8:23 am

    But after I select a new item and scroll back and forth I see wierd
    behavior, the check image is sometimes visible in multiple list items
    or is hidden in the actual item that is currently selected.

    That’s most likely happening because of the way you set the click listener for convertView. You set the OnCLickListener only for the case when convertView is null but as you scroll the ListView up and down, rows will get recycled and you would end up with the same listener as other rows.

    Anyway if you just want to have only one row with the image visible there is a much simpler way to do it. First of all you should drop the OnCLickListener on the convertView and use the OnItemClickListener on your ListView element. From this listener callback you’ll modify the rows:

    lstview.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> l, View v, int position,
                        long id) {
                    View oldView = l.getChildAt(adap.getSelectedPosition());
                    ImageView img;
                    if (oldView != null && adap.getSelectedPosition() != -1) {
                        img = (ImageView) oldView.findViewById(R.id.imageView1);
                        img.setVisibility(View.INVISIBLE);
                    }
                    img = (ImageView) v.findViewById(R.id.imageView1);
                    img.setVisibility(View.VISIBLE);
                    adap.setSelectedPosition(position);
                }
            });
    

    Then modify the adapter like this:

    // a field in the adapter
    private int mSelectedPosition = -1;
    
    // getter and setter methods for the field above
    public void setSelectedPosition(int selectedPosition) {
        mSelectedPosition = selectedPosition;
        notifyDataSetChanged();
    }
    
    public int getSelectedPosition() {
        return mSelectedPosition;
    }
    
    // and finally your getView() method
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
          convertView = mInflater.inflate(R.layout.adaptor_content, parent, false);
          holder = new ViewHolder();
          holder.textLine = (TextView) convertView.findViewById(R.id.txtCategoryCaption);
          holder.iconLine = (ImageView) convertView.findViewById(R.id.iconLine);
          holder.imgCheckbox = (ImageView) convertView.findViewById(R.id.imgCheck);
          convertView.setTag(holder);
        } else {
          holder = (ViewHolder) convertView.getTag();
        }
        if (mSelectedPosition == position) {
        holder.imgCheckbox.setVisibility(View.VISIBLE);
        } else {
        holder.imgCheckbox.setVisibility(View.INVISIBLE);
        }     
          // what is the point of this call?!
          // you should move this to another place(like the adapter's constructor), the getIdentifier()
          // method is a bit slow and you call it each time the adapter calls getView()
          // you should never use it in the getView() method(), especially as all you do is get the id of the same drawable again and again
          int id = context.getResources().getIdentifier("nodeinsert", "drawable", context.getString(R.string.package_str));
          // ?!?
          if (id != 0x0) {
              mIcon1 = BitmapFactory.decodeResource(context.getResources(), id);
          }
          holder.iconLine.setImageBitmap(mIcon1);
          holder.textLine.setText(String.valueOf(data[position]));
          return convertView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented an adapter for my ListView which extends BaseAdapter. My list items
I have a listview which is filled with data form sqlitedatabase. A list-view element
I have ListView which is saving all data to database. For adding i have
I have a ListView which displays a list of string values. I want to
I have a Custom ListView which has an ImageView and a TextView. and i
I have a listview which includes 2 textviews and 1 imageview. Now, the image
I have implemented Section Indexer for an Adapter class which extends BaseAdapter. Now for
I have a listview which populates its content from SQLite Database. Here's my code:
i have a ListView for which i have made a custom adapter which extends
I have made a custom listView which which extends ListActivity and each row contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.