0

After researching / reviewing code the past couple days, a solution eludes me. The ArrayAdapter.getView() function is not being called.

ArrayAdapter.getCount() is returning 3.

This listview is similar to the WiFi preference fragment .. https://android.googlesource.com/platform/packages/apps/Settings.git/+/6829c48c36fceebb46989f8ee25c369fa5f7adeb/src/com/android/settings/wifi/WifiSettings.java

The code samples at GitHub (using title search terms) show that this should be working.

Queries through stackoverflow for "getView not called" have suggested that

  • the List was not being passed in,
  • the count was not being returned,
  • the ArrayAdapter was not attached to the ListView properly using setListAdapter vs setAdapter
  • the ArrayAdapter and ListView scope need to be class vs method level
  • 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.

Provided below are the PreferenceFragment and ArrayAdapter Classes, and XML files.

Your Thoughts?

PreferenceFragment Class

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<String> strings = new ArrayList<String> ();
    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

ArrayAdapter Class

private class MyAdapter extends ArrayAdapter<String>
{
    Context context;
    List<String> strings = new ArrayList<String>();
    int layoutResourceId;

    public MyAdapter(Context context, int layoutResourceId, List<String> 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;
    }       
}
}

Preference Header Segment

<header
    android:fragment="ENetArch.Todo.Settings_WebServers"
    android:icon="@drawable/icon_server"
    android:title="WebServers"
    android:summary="A list of servers to synchronize with"/>

xml.settings.webservers.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

</PreferenceScreen>

layout.webservers.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     />

</LinearLayout>

layout.webservers_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <TextView
        android:id="@+id/txtServerName"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:text="ENetArch"
        />

</LinearLayout>

Log Trace

04-25 15:33:02.840: D/onCreateView(2941): Entered Function

04-25 15:33:02.930: D/MyAdapter(2941): construct entered

04-25 15:33:02.930: D/MyAdapter(2941): construct exited

04-25 15:33:02.950: D/getCount(2941): 3

04-25 15:33:02.950: D/getCount(2941): 3

04-25 15:33:02.950: D/onCreateView(2941): Exiting Function

04-25 15:33:03.070: D/onStart(2941): Entered Function

04-25 15:33:03.070: D/ListView(2941): hidden

E Net Arch
  • 458
  • 5
  • 13
  • Considering the last possible reason for ArrayAdapter.getView not being called, I added a border to ListView, using [link] (http://stackoverflow.com/questions/5065722/how-do-you-put-a-border-around-a-listview). A red border appeared, filled the screen, and no contents appeared. – E Net Arch Apr 25 '14 at 18:56
  • added the following code: ` @Override public void onStart() { super.onStart();; Log.d ("onStart ", "Entered Function" ); Log.d ("ListView ", String.valueOf(lv.isShown()?"shown":"hidden")); } ` – E Net Arch Apr 25 '14 at 19:34
  • Created a new activity, WebServersActivity, from the code above, the only thing that really changed was "setContentView (R.layout.webservers);". The activity displayed, and my 3 strings appeared as they were supposed to. Very Strange as to what this error is. =( – E Net Arch Apr 26 '14 at 16:22

0 Answers0