没有调用android preferencefragment listview arrayadapter getview

时间:2014-04-25 17:09:10

标签: android-listview preferencefragment getview

在过去几天研究/审查代码之后,我找不到解决方案。未调用ArrayAdapter.getView()函数。

ArrayAdapter.getCount()返回3。

此列表视图类似于WiFi偏好片段.. https://android.googlesource.com/platform/packages/apps/Settings.git/+/6829c48c36fceebb46989f8ee25c369fa5f7adeb/src/com/android/settings/wifi/WifiSettings.java

GitHub上的代码示例(使用标题搜索术语)表明这应该有效。

通过stackoverflow查询" getView未调用"有人建议

  • 列表未被传递,
  • 计数未被退回,
  • 使用setListAdapter vs setAdapter
  • 未正确将ArrayAdapter附加到ListView
  • ArrayAdapter和ListView范围需要是类与方法级别
  • ListView可能不可见,因此android知道它无法在屏幕上绘制它,因此永远不会调用getView()方法来渲染对象。

下面提供了PreferenceFragment和ArrayAdapter类以及XML文件。

你的想法?

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类

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;
    }       
}
}

首选项标题细分

<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>

记录跟踪

04-25 15:33:02.840:D / onCreateView(2941):输入函数

04-25 15:33:02.930:D / MyAdapter(2941):构造已进入

04-25 15:33:02.930:D / MyAdapter(2941):构建退出

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):退出功能

04-25 15:33:03.070:D / onStart(2941):输入功能

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

0 个答案:

没有答案