简体   繁体   中英

Android ListView: Do I need to create another class to use it?

I have an app using an ScrollView and it loads a lot of images.

I decided to change to ListView. I saw that to use ListView, my class must extend ListActivity.

My current class does a lot of things, like inserting in database, updating, etc.

So, should I create a new classe just for the ListView? Can I just say that my current class now extends ListActivity?

In case I need to create a separate class just for the ListView, how do I use it in my current class?

Any help is appreciated!

Best regards

Your class doesn't necessary have to extend ListActivity you could use a simple Activity with a ListView element in it's layout. To use your existing activity just replace the current ScrollView element with a ListView element. Then retrieve this element in onCreate() and set its adapter that will map the images to the ListView rows. You will have to make your own custom adapter to show the images.

ListActivity is just a sub-class of Activity.

Its the same but it allows you to have utility methods such as setListAdapter() and also manages stuff like showing a placeholder when there is nothing in the list by looking for @android:id/empty in your inflated layout.

Using ListActivity it helps to manage the List that with the id of @android:id/list. It works just as well managing your own list without the ListActivity,it wont break your current code if used correctly just have to make an adapter for the list..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM