Question

I have 10 Fragments and each having ListView. I am displaying data from custom adapter. But ListView is not displaying anything. Example:- suppose i am in page-no 4 and there is button. i clicked on it and some data fetched from server and after that i am using custom adapter. but data is not displaying in ListView.

I noted, If i replace my listview from onCreateView() into onActivityCreated(). It works. and i am able to see data at ListView. But each page is updating to listview of pageno-1. suppose i am in page no-5 and updating to listview. here listview of pageno-5 is not displaying data. i saw that it displaying data at page no-1's listview.

I think, Fragment is not able to add listview in all pages so that data will be display per page. after adding data in listview if i touch in listiview getting error :-

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

myAdapter adapter = new myAdapter(getActivity().getBaseContext(),
list);
swingListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Was it helpful?

Solution

Are you adding and removing ListViews? If so, that's the wrong approach. When the new data comes in from the server, you need to operate on the Adapter to refresh the data. Something like

 function void onLoadFinished( List<?> myObjects )
 {
      this.adapter.clear();
      this.adapter.addAll( myObjects );
 }

This will instantly clear the list and add your new data. Constructing the ListView each time is not the way to go.

Also, you probably should consider using the Loader framework, as it will handle all of the AsyncTask stuff for you. I learned the pattern using a ADP's excellent examples here.

Also, make sure you're extending ListFragment. If so, your layout needs to have a ListView in it that has the id @id/android:list, then just set it in the onCreateView method. Here's an example XML:

<!DOCTYPE android>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:cacheColorHint="#FDFFCE" />

    <Spinner
        android:id="@+id/location_spinner"
        android:layout_width="56dp"
        android:layout_height="1px"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:textColor="#FF000000"
        android:visibility="invisible" />

    <Spinner
        android:id="@+id/area_spinner"
        android:layout_width="wrap_content"
        android:layout_height="1px"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/location_type_spinner"
        android:gravity="center"
        android:textColor="#FF000000"
        android:visibility="invisible" />    

</RelativeLayout>

then the view is inflated here:

@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
    View view = inflater.inflate( R.layout.fragment_location_list, null );
    return view;
}

I do all additional setup and view binding in 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 );        
    .
    .
    .
}

That should take care of all the FragmentActivity lifecycle events for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top