How to Select ListView Item Programmatically
<p>Question: How do I highlight an option in a ListView programmatically?</p>

<p>Background: I have looked at tons of threads for this kind of issue however none seem to work. I have a list view in a fragment that is populated by a list created from a database using a custom adapter. When the fragment is opened I would like one of the items to be selected by default (depending on whether edit mode is 1/enabled), I have the position of this item ready. I currently use the <code>clientList.setChoiceMode(ListView.CHOICE_MODE_SINGLE)</code>, and try to highlight the option with <code>setItemChecked</code> but it doesn't work.</p>

<p><strong>Fragment OnCreate</strong></p>

<pre><code>@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    FragmentActivity faActivity = (FragmentActivity) super.getActivity();
    final LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.fragment__client, container, false);

    addJob = (AddJob)getActivity();

    clientList = (ListView) linearLayout.findViewById(R.id.listView);

    clientList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    dbHandler = new DraycottDataHandler(super.getActivity().getApplicationContext());

    if (dbHandler.getClientCount() != 0)
        Clients.addAll(dbHandler.getAllClients()); //Loads clients into Client list

    populateList(); //Sets adapter for the listview.


    if(addJob.getEdit() == 1) { //Get edit mode from activity
        NewJobClass editJob = addJob.getEditJob();


        for (int i = 0; i &lt; Clients.size(); i++) {
            ClientClass client = Clients.get(i);

            if (client.getID() == editJob.getClientID()) {
                clientList.setItemChecked(i, true); //Highlight item (doesnt do anything).
            }
        }
    }


    clientList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            selectedClient = Clients.get(position);
            listener.getClientData(selectedClient);

        }
    });


    return linearLayout;

}
</code></pre>

<p><strong>ListView XML</strong></p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.simple.jack.application.Fragment_Client"&gt;

    &lt;ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:choiceMode="singleChoice"
        android:listSelector="#D3D3D3"
        android:clickable="true"/&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>If more code is needed I'd be glad to provide it.</p>

<p><a href="https://stackoverflow.com/questions/10788688/programmatically-select-item-listview-in-android">Similar Question and Answer</a></p>

<p>This question is similar to mine however the answer gives me an InflateException.</p>

<p>Thanks in advance for any help. </p>