Android:未调用自定义适配器的getView方法

Android:未调用自定义适配器的getView方法,android,listview,adapter,Android,Listview,Adapter,对于同一个问题,我已经搜索了很多其他答案,但没有找到任何适合我的解决方案。正如标题所说,问题在于我的自定义适配器中的getView方法没有被调用 下面是代码(首先是片段): 下面是适配器: public class CategoryListAdapter extends ArrayAdapter<CategoryElement> { private LayoutInflater mInflater; public CategoryListAdapter(Context ctx)

对于同一个问题,我已经搜索了很多其他答案,但没有找到任何适合我的解决方案。正如标题所说,问题在于我的自定义适配器中的getView方法没有被调用

下面是代码(首先是片段):

下面是适配器:

public class CategoryListAdapter extends ArrayAdapter<CategoryElement> {

private LayoutInflater mInflater;

public CategoryListAdapter(Context ctx) {
    super(ctx, R.layout.category_element);

    mInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view;

    Log.d("Adapter", "Restituisco la view per l'elemento");

    if (convertView == null) {
        view = mInflater.inflate(R.layout.category_element, null);
    } else {
        view = convertView;
    }

    //((TextView) view.findViewById(R.id.category_element_text)).setText(getItem(position).getName());
    TextView textView = (TextView) view.findViewById(R.id.category_element_text);
    textView.setText(getItem(position).getName());

    return view;
}
我换了衣服

fragmentTransaction.add(R.id.activity_main, categoryListFragment);
如果不指定片段应附加到的视图id,它将永远不会绘制它
除此之外,我不得不改变

view = mInflater.inflate(R.layout.category_element, parent);
为此:

view = mInflater.inflate(R.layout.category_element, null);
在getView方法中。

PS.我正在编辑此项,因为在8小时过去之前我无法回答自己的问题。

我认为在您的
R.layout.category\u list
文件中,您需要将ListView赋予以下属性:

android:id=“@android:id/list”

ListFragment(和ListActivity)在调用方法(如
setListAdapter()
)时查找此id以查找
ListView


此外,如果您只需要一个ListView,则不必提供布局文件。只要不重写onCreateView(),系统就会自动为您提供一个
ListView
。只有当您想要自定义布局时,才需要对其进行充气,如果需要,ListView应该具有上面所述的id。

您在logcat中找到了与适配器相关的任何内容吗?可能是您的适配器实际上正在加载,但没有返回任何内容来加载适配器
TextViews
。也许您可以检查或记录
getItem(position)
是否实际返回任何内容。否则,您可以只要求您的“项”通过构造函数传递,并像通常那样在适配器中作为字段公开它们。我还尝试将getItem放在getCount之后,它会返回正确的CategoryElement对象…是否有可能
CategoryListFragment.onCreateView
干扰了
CategoryListAdapter.getView
?这可能是一个愚蠢的评论,我只是在头脑风暴。不知道,但我对此表示怀疑。然而,我刚刚注意到onCreateView中的容器总是空的。这可能是问题所在吗?我想找点东西,但还是没什么。我觉得这不重要。事实上,我没有在我的布局中指定任何类似的内容,就让它工作了。无论如何谢谢你!
fragmentTransaction.add(0, categoryListFragment);
fragmentTransaction.add(R.id.activity_main, categoryListFragment);
view = mInflater.inflate(R.layout.category_element, parent);
view = mInflater.inflate(R.layout.category_element, null);