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

  • SEARCH
  • Home
  • 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 8634251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:40:46+00:00 2026-06-12T09:40:46+00:00

I have an Activity that hosts multiple fragments using the actionbar’s tab functionality. One

  • 0

I have an Activity that hosts multiple fragments using the actionbar’s tab functionality. One of those fragments contains a ListView. Upon this tab being selected, I’d like to select a certain item.

To do this programmatically, I use the following code (where calls is the ListView)

private void selectItem(int position)
{
    long itemId = calls.GetItemIdAtPosition(position);
    calls.PerformItemClick(calls, position, itemId);
}

If this ListView has been rendered, and I’m calling this, no problem. However, if I call it from onResume, then the code executes but nothing is selected in the end. I figure this is because at the point where I’m calling selectItem, not all items of the ListView have been rendered yet. If however I start off a background thread, sleep for a couple hundred milliseconds, then run the same code (in the ui thread of course), everything is fine, but this is an ugly hack.

Now you might be wondering, “why isn’t he using calls.setSelection”? The thing is, I’m using a custom layout that performs expansion – so I need to actually click on the item I want selected (which in turn triggers the layout expansion for the item selected). However, I can call the code that is performed on PerformItemClick directly, the results will be the same (the layout expansion isn’t performed).

Isn’t there any way for me to catch the “Listview has finished rendering all viewable items” point in time, and then execute my selectItem call at that point? In ASP.NET, I have an event on every UI item telling me when it is done rendering, so I do item selection at that point but I haven’t found anything.

Regards
Stephan

Here’s the Adapter I’m using

public class ActiveCallsAdapter: ObservableAdapter<Call>
{

    public ActiveCallsAdapter(Activity activity, ObservableCollection<Call> calls)
        : base(activity, calls)
    {
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];
        var view = (convertView ?? context.LayoutInflater.Inflate(Resource.Layout.Call, parent, false)) as LinearLayout;
        //View view = convertView;
        //if (view == null) // no view to re-use, create new
        //    view = context.LayoutInflater.Inflate(Resource.Layout.Call, null);

        SetTextView(view, Resource.Id.CallerName, item.CallerName);
        SetTextView(view, Resource.Id.CallerNumber, item.CallerNumber);
        SetTextView(view, Resource.Id.CallStatus, item.State.ToString());
        SetTextView(view, Resource.Id.CallDuration, item.Duration);

        return view;
    }

    public void Update(LinearLayout view, Call item)
    {
        SetTextView(view, Resource.Id.CallerName, item.CallerName);
        SetTextView(view, Resource.Id.CallerNumber, item.CallerNumber);

        string identifier = "callState_" + item.State.ToString();
        int resourceId = Application.Context.Resources.GetIdentifier(identifier, "string", Application.Context.PackageName);
        string callStateString = item.State.ToString();
        if (resourceId != 0)
        {
            try
            {
                callStateString = Application.Context.Resources.GetString(resourceId);
            }
            catch (Exception e)
            {
                AndroidLogModel.Model.AddLogMessage("ActiveCallsAdapter", "Unable to find call state string with resource id " + resourceId + " state string: " + identifier, 3);
            }
        }
        SetTextView(view, Resource.Id.CallStatus, callStateString);
        //SetTextView(view, Resource.Id.CallDuration, item.Duration);
    }

    public void UpdateDuration(LinearLayout view, Call item)
    {
        SetTextView(view, Resource.Id.CallDuration, item.Duration);
    }

}

And the base class of that adapter

    public class ObservableAdapter<T>: BaseAdapter<T>
{

    protected readonly Activity context;
    protected readonly ObservableCollection<T> items;

    public ObservableAdapter(Activity context, ObservableCollection<T> collection)
    {
        this.context = context;
        this.items = collection;
        //this.collection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collection_CollectionChanged);
        this.items.CollectionChanged += (sender, e) => NotifyDataSetChanged();
    }

    void collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        NotifyDataSetChanged();
    }

    public override T this[int position]
    {
        get { return items[position]; }
    }

    public override int Count
    {
        get { return items.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];
        var view = (convertView ?? context.LayoutInflater.Inflate(Resource.Layout.Call, parent, false)) as LinearLayout;
        // configure view here
        return view;
    }

    protected void SetTextView(LinearLayout view, int id, string text)
    {
        var textView = view.FindViewById<TextView>(id);
        if (textView != null)
            textView.SetText(text, TextView.BufferType.Normal);
    }
}
  • 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-12T09:40:47+00:00Added an answer on June 12, 2026 at 9:40 am

    My Mono skills are limited so I don’t know if I fully understood your adapter, anyway I’ve adapted some old code and made an adapter that expands a single item when click, also it will move the ListView in onResume to a desired position:

    private static class CustomAdapter extends BaseAdapter {
    
            // the data
            private ArrayList<String> mData;
    
            // an int pointing to a position that has an expanded layout,
            // for simplicity I assume that you expand only one item(otherwise use
            // an array or list)
            private int mExpandedPosition = -1; // -1 meaning no expanded item
            private LayoutInflater mInflater;
    
            public CustomAdapter(Context context, ArrayList<String> items) {
                mInflater = LayoutInflater.from(context);
                mData = items;
            }
    
            public void setExpandedPosition(int position) {
                // if the position equals mExpandedPosition then we have a click on
                // the same row so simply toggle the row to be gone again
                if (position == mExpandedPosition) {
                    mExpandedPosition = -1;
                } else {
                    // else change position of the row that was expanded
                    mExpandedPosition = position;
                }
                // notify the adapter
                notifyDataSetChanged();
            }
    
            @Override
            public int getCount() {
                return mData.size();
            }
    
            @Override
            public String getItem(int position) {
                return mData.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.ad_expandedelement,
                            parent, false);
                }
                ((TextView) convertView.findViewById(R.id.textView1))
                        .setText(getItem(position));
                // see if there is an expanded position and if we are at that
                // position
                if (mExpandedPosition != -1 && mExpandedPosition == position) {
                    // if yes simply expand the layout
                    convertView.findViewById(R.id.button1).setVisibility(
                            View.VISIBLE);
                } else {
                    // this is required, we must revert any possible changes
                    // otherwise the recycling mechanism will hurt us
                    convertView.findViewById(R.id.button1).setVisibility(View.GONE);
                }
                return convertView;
            }
    
        }
    

    The onListItemClick will simply be:

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // set the expanded(or collapsed if it's a click on the same row that
        // was previously expanded) row in the adapter
        ((CustomAdapter) getListView().getAdapter())
                .setExpandedPosition(position);
    }
    

    and in onResume will have:

    @Override
    protected void onResume() {
        super.onResume();
        // set the position to the desired element
        ((CustomAdapter) getListView().getAdapter()).setExpandedPosition(15);
        // set the selection to that element so we can actually see it
        // this isn't required but has the advantage that it will move the
        // ListView to the desired
        // position if not visible
        getListView().setSelection(15);
    }
    

    The R.layout.ad_expandedelement is a simple vertical LinearLayout with a TextView and an initially hidden(visibility set to gone) Button. For this Button I change the visibility to simulate expanding/collapsing a row in the ListView. You should be able to understand my code, if you want I can post on github the full sample.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i currently have an Activity that hosts a tab view, and each tab
I have an activity that appears as a dialog using the following custom theme:
I have an activity that contains several user editable items (an EditText field, RatingBar,
I have a TabActivity that hosts other three activities. If the first tab is
Consider that i have a maintenance activity on my dataserver which hosts a lot
I have an FragmentActivity that uses the ActionBar Tabs, one of the tabs has
I have an application that is a Tab activity with various tabs that load
I have a multiple Activity application that progresses the user from entering an IP/Host
I am making a activity using Tab-Host. I have two tabs. When I start
I have an activity that when users press share on a file it would

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.