Android ListView未在片段中显示数据

Android ListView未在片段中显示数据,android,listview,fragment,Android,Listview,Fragment,我有10个片段,每个片段都有ListView。我正在显示来自自定义适配器的数据。但是ListView没有显示任何内容。示例:-假设我在第4页,有一个按钮。我点击了它,并从服务器获取了一些数据,之后我使用了自定义适配器。但数据未显示在ListView中 我注意到,如果我将listview从onCreateView()替换为onActivityCreated()。它起作用了。我可以在ListView中看到数据。但是每个页面都在更新到pageno-1的listview。 假设我在第5页,正在更新到li

我有10个片段,每个片段都有ListView。我正在显示来自自定义适配器的数据。但是ListView没有显示任何内容。示例:-假设我在第4页,有一个按钮。我点击了它,并从服务器获取了一些数据,之后我使用了自定义适配器。但数据未显示在ListView中

我注意到,如果我将listview从onCreateView()替换为onActivityCreated()。它起作用了。我可以在ListView中看到数据。但是每个页面都在更新到pageno-1的listview。 假设我在第5页,正在更新到listview。此处pageno-5的listview不显示数据。我看到它在第1页的列表视图中显示数据

我认为,Fragment不能在所有页面中添加listview,以便每页显示数据。在listview中添加数据后,如果我触摸listview获取错误:-

04-30 16:03:40.120: W/AbsListView(8161): Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 0,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@42236718
04-30 16:03:40.159: W/AbsListView(8161): Intent to operate on non-exist data, childcount = 0,mFirstPosition = 0,adapter count = 0,action = 2,mActivePointerId = 0,mScrollY = 0,this = android.widget.ListView@42236718


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    mView = inflater.inflate(R.layout.scorecard_swing_items,
            container, false);

    swingListView = (ListView) mView.findViewById(R.id.swingListView);
    return mView;
)
onActivityResult()


是否添加和删除列表视图?如果是这样,那就错了。当新数据从服务器传入时,您需要在适配器上操作以刷新数据。差不多

 function void onLoadFinished( List<?> myObjects )
 {
      this.adapter.clear();
      this.adapter.addAll( myObjects );
 }
然后,视图在此处膨胀:

@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
    View view = inflater.inflate( R.layout.fragment_location_list, null );
    return view;
}
我在onActivityCreated中执行所有附加设置和视图绑定:

@Override
public void onActivityCreated( Bundle savedInstanceState )
{
    super.onActivityCreated( savedInstanceState );
    setRetainInstance( true );

    LayoutInflater.from( getActivity() );
    getListView().setDividerHeight( 1 );
    getListView().setOnCreateContextMenuListener( this );
    getListView().setOnItemLongClickListener( this );
    getListView().setFocusable( true );
    getListView().setOnTouchListener( this );        
    .
    .
    .
}

这将为您解决所有的活动生命周期事件。

我正在尝试使用我的观察技能来查看您的代码有什么问题,但愿景没有clear@Mikael请检查我的更新,但Listview第一次未显示任何数据时。是否扩展ListFragment?如果是,XML中应该有一个列表视图,其id为
@id/android:list
。下面是我的一个应用程序的一个示例:我正在扩展片段。在xml中获取一个listview,并将其称为onCreateView()swingListView=(listview)mView.findViewById(R.id.swingListView);我添加了一些示例代码。我建议您扩展ListFragment。它指示活动不要在旋转时重建片段。它只会在屏幕旋转时调用
onAttach
onDetach
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
    View view = inflater.inflate( R.layout.fragment_location_list, null );
    return view;
}
@Override
public void onActivityCreated( Bundle savedInstanceState )
{
    super.onActivityCreated( savedInstanceState );
    setRetainInstance( true );

    LayoutInflater.from( getActivity() );
    getListView().setDividerHeight( 1 );
    getListView().setOnCreateContextMenuListener( this );
    getListView().setOnItemLongClickListener( this );
    getListView().setFocusable( true );
    getListView().setOnTouchListener( this );        
    .
    .
    .
}