0

在过去几天研究/审查代码之后,我没有找到解决方案。未调用 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 not called”表明

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

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

你的意见?

PreferenceFragment 类

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>

布局.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): 隐藏

4

0 回答 0