Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

android - What to do about ListActivity/MapActivity when converting to Fragments using the compatibility library?

I'm converting an existing app to the Fragments API using the compatibility library. I've read that you're supposed to update classes that extend Activity to now use FragmentActivity. This is fine for most cases, but what about classes that extend ListActivity or MapActivity? What is the preferred way to handle this? I was hoping there was a ListFragmentActivity or something along those lines, but I don't see one.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is what I do when converting a ListActivity to the fragments API:

  1. Replace lv = getListView(); with lv = (ListView) findViewById(android.R.id.list);

  2. Replace setListAdapter(adapter); with lv.setAdapter(adapter);

  3. If you have overriden onListItemClick(), replace it with lv.setOnItemClickListener(new ListView.OnItemClickListener() {...

  4. You'll have to set the empty view (that shows when there are no results) manually: lv.setEmptyView(findViewById(android.R.id.empty));

    If I'm using CursorLoader, I normally put this in onLoadFinished():

    // if there are no results
    if (data.getCount() == 0) {
        // let the user know
        lv.setEmptyView(findViewById(android.R.id.empty));
    } else {
        // otherwise clear it, so it won't flash in between cursor loads
        lv.setEmptyView(null);
    }
    
  5. Speaking of cursor loaders, I'll also convert the activity to use CursorLoader if it isn't already by that point


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...