title: android preferencefragment listview arrayadapter getview not called
body: <p>After researching / reviewing code the past couple days, a solution eludes me. The ArrayAdapter.getView() function is not being called.  </p>
  
  <p>ArrayAdapter.getCount() is returning 3.</p>
  
  <p>This listview is similar to the WiFi preference fragment ..
  <a href="https://android.googlesource.com/platform/packages/apps/Settings.git/+/6829c48c36fceebb46989f8ee25c369fa5f7adeb/src/com/android/settings/wifi/WifiSettings.java" rel="nofollow">https://android.googlesource.com/platform/packages/apps/Settings.git/+/6829c48c36fceebb46989f8ee25c369fa5f7adeb/src/com/android/settings/wifi/WifiSettings.java</a></p>
  
  <p>The code samples at GitHub (using title search terms) show that this should be working.</p>
  
  <p>Queries through stackoverflow for "getView not called" have suggested that </p>
  
  <ul>
  <li>the List was not being passed in, </li>
  <li>the count was not being returned, </li>
  <li>the ArrayAdapter was not attached to the ListView properly using setListAdapter vs setAdapter</li>
  <li>the ArrayAdapter and ListView scope need to be class vs method level</li>
  <li>the ListView may not be visible, thus android knows it would not be able to draw it on the screen so getView() method is never called to render the objects.</li>
  </ul>
  
  <p>Provided below are the PreferenceFragment and ArrayAdapter Classes, and XML files.</p>
  
  <p>Your Thoughts?</p>
  
  <p><strong>PreferenceFragment Class</strong></p>
  
  <pre><code>public class Settings_WebServers extends PreferenceFragment
  {
  
  protected MyAdapter adapter = null;
  protected ListView lv = null;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate (savedInstanceState);
      addPreferencesFromResource(R.xml.settings_webservers);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
  {
      super.onCreateView (inflater, container, savedInstanceState);
  
      View v = inflater.inflate (R.layout.webservers, container, false);
  
      List&lt;String&gt; strings = new ArrayList&lt;String&gt; ();
      strings.add ("WebServer 1");
      strings.add ("WebServer 2");
      strings.add ("WebServer 3");
  
      adapter = new MyAdapter (container.getContext(), R.layout.webservers_list, strings);
  
      lv = (ListView) v.findViewById (android.R.id.list);
  
      lv.setAdapter (adapter);
  
      return v;
  }
  
  @Override
  public void onStart()
  {
      super.onStart();;
      Log.d ("onStart ", "Entered Function" );
      Log.d ("ListView ", String.valueOf(lv.isShown()?"shown":"hidden"));
  }
  
  // ArrayAdapter embedded class
  </code></pre>
  
  <p><strong>ArrayAdapter Class</strong></p>
  
  <pre><code>private class MyAdapter extends ArrayAdapter&lt;String&gt;
  {
      Context context;
      List&lt;String&gt; strings = new ArrayList&lt;String&gt;();
      int layoutResourceId;
  
      public MyAdapter(Context context, int layoutResourceId, List&lt;String&gt; objects)
      {
          super(context, layoutResourceId, objects);
  
          this.layoutResourceId = layoutResourceId;
          this.strings = objects;
          this.context = context;
      }
  
      @Override
      public int getCount ()
      { return (strings.size()); }
  
      @Override
      public View getView (int position, View convertView, ViewGroup parent)
      {
          Log.d ("getView position", String.valueOf (position) );
  
          TextView lblServerName = null;
  
          String current = strings.get (position);
  
          LayoutInflater inflater = 
              (LayoutInflater) context.getSystemService 
              (Context.LAYOUT_INFLATER_SERVICE);
  
          convertView = inflater.inflate 
              (R.layout.webservers_list, parent, false);
  
  
          lblServerName = (TextView) convertView.findViewById (R.id.txtServerName);
          if (lblServerName != null)
              lblServerName.setText (String.valueOf(current));
  
          return convertView;
      }       
  }
  }
  </code></pre>
  
  <p><strong>Preference Header Segment</strong></p>
  
  <pre><code>&lt;header
      android:fragment="ENetArch.Todo.Settings_WebServers"
      android:icon="@drawable/icon_server"
      android:title="WebServers"
      android:summary="A list of servers to synchronize with"/&gt;
  </code></pre>
  
  <p><strong>xml.settings.webservers.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;PreferenceScreen 
  xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"&gt;
  
  &lt;/PreferenceScreen&gt;
  </code></pre>
  
  <p><strong>layout.webservers.xml</strong></p>
  
  <pre><code>&lt;LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"&gt;
  
  &lt;ListView
      android:id="@android:id/list"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
       /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>layout.webservers_list.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="?android:attr/listPreferredItemHeight"
      android:padding="6dip"&gt;
  
      &lt;TextView
          android:id="@+id/txtServerName"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:layout_width="match_parent"
          android:text="ENetArch"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Log Trace</strong></p>
  
  <p>04-25 15:33:02.840: D/onCreateView(2941): Entered Function </p>
  
  <p>04-25 15:33:02.930: D/MyAdapter(2941): construct entered </p>
  
  <p>04-25 15:33:02.930: D/MyAdapter(2941): construct exited </p>
  
  <p>04-25 15:33:02.950: D/getCount(2941): 3 </p>
  
  <p>04-25 15:33:02.950: D/getCount(2941): 3 </p>
  
  <p>04-25 15:33:02.950: D/onCreateView(2941): Exiting Function </p>
  
  <p>04-25 15:33:03.070: D/onStart(2941): Entered Function </p>
  
  <p>04-25 15:33:03.070: D/ListView(2941): hidden </p>