Android - Activity vs. ListActivity - Which one should my activity class extend?

I've been learning to develop in Android and had more of a general question: If I have a layout that uses a list and some other views, should I be using Activity or ListActivity for my activity class?

I know that ListActivity will probably make things easier to override list-specific events, but is there any other advantage to using a ListActivity over Activity? What if I wanted to change the layout in the future to a GridView? Would it be any more/less of a pain to change the class' code?

I was just curious about "best practices" in this regard and what the benefits entail, so any answer would be helpful :)

Bara


Asked by: Thomas476 | Posted: 20-01-2022






Answer 1

I'd use a ListActivity since it gives you a lot of shortcut methods to make things easier and keep your code more readable.

For example, you get the onListItemClick() method which is called whenever you click a item which saves you from creating a separate listener.

If you want to change the layout of a ListActivity you still can with setContentView() method from Activity. As long as there is a ListView called @android:id/list somewhere in your View the ListActivity will still work.

If you're still not sure, you could always look at the source code for ListActivity and see that it doesn't do all that much other than make your life a little easier.

Answered by: Caroline375 | Posted: 21-02-2022



Answer 2

I'd go with Activity. Don't really see the reason to use ListActivity, unless you want to do something trivial and you know it's gonna be with the List.

Answered by: Lenny911 | Posted: 21-02-2022



Answer 3

You should use Activity rather than ListActivity because ListActivity codes are non-reusable and you need to write them again and again.

Answered by: Maria353 | Posted: 21-02-2022



Similar questions

android - How come when the activity is on ListActivity, it crashes but when it's activity, it works fine

public class test extends ListActivity{ /** Called when the activity is first created. */ protected Dialog mSplashDialog; private static final String[] country = { "Iceland", "India", "Indonesia","Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia","Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania","Luxembourg" }; private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD","EUR", "ILS", "...


android - Share Same code in Activity and ListActivity

My project consists of couple of activity and ListActivity items, there is some common piece of code(Navigation bar and some other codes) which needs to be done on both type of activity. Is there a way I extend the activity and write my piece of code, and let ListActivity also inherent that code ? right now I am copying the same piece of code in two classes , one is Activity extended and other is ListActivi...


android - Using an intent to start new activity from ListActivity

my ListActivity has a button on the bottom that I want to use to fire up a new activity using an intent. When I tried saying Intent intent = new Intent(this, OtherActivity.class) I'm getting an error that the constructor is undefined. FWIW, the other activity extends MapActivity. Not sure if that matters... What do I need to do to get this to work?


android - How come when the activity is on ListActivity, it crashes but when it's activity, it works fine

public class test extends ListActivity{ /** Called when the activity is first created. */ protected Dialog mSplashDialog; private static final String[] country = { "Iceland", "India", "Indonesia","Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia","Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania","Luxembourg" }; private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD","EUR", "ILS", "...


android - Add a ListActivity in Activity

I would like to add a ListActivity in Activity. For Example, there's a title and a button on top of the page of the Activity, and the ListActivity is the main content of the Activity. The button would be able to load different ListActivity below. When swiping left and right, there will be new Activitys and new content...


Android ListActivity different title and activity name

I'd like to show the user a list of different available activities in my app, but I want the names to be explicit such as "Accelerometer Control", or "Line Follower". Right now the activities are stored in a String array, these activity names are what are shown AND called. I'd like to show "Line Follower" and call the "LineFollower" activity (without space). I haven't found any examples doing that. Her...


java - Why do i get "source not found" when I change extends Activity to extends ListActivity?

I've got a problem with this class. This Activity will be called when I press a button. When I extend Activity to this class the programm gets into the class, but when I extend ListActiviy and I click the button the debugger tells me in red words "source not found" and nothing else, not even something in the logcat. Please tell me if there is something missing in the xml-file of this activity or the manifest. Thi...


How to pass a string from activity to ListActivity in Android?

I want to pass a String from Activity to ListActivity in Android. When I use normal Activty to Activity String pass method it gives NullPointerException. Here is what I tried to do. Activity Intent intent = new Intent(CheckAvailability.this , ListAtmActivity.class); intent.putExtra("key", b_name); startActivity(intent); And I tried to get this from my ListActivity as t...


android - unable to start listactivity in another activity

In my android app, there is a xml file called mylistview : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" ...


android - Sending an adapter from Activity to ListActivity

I dont know whether my present approach is correct or not... I am having two classes one that extends the Activity and the other that extends the ListActivity. Now I wanted to sort my list in the ListActivity when I press a button present in the Activity class layout. What are the possible ways.. presently I am trying to send an adapter from the Activity class to ListActivity but it is failing me. So any other ideas. Expec...


android - passing data from an activity to an listactivity or listview

need help on passing data from an activity to an listactivity or listview for an android app. im having problems on passing data to a listview. what the app do is from addact class the user can input things to do and in the viewact class this will display the activies add by the user in listview public class addact extends Activity { @Override public void onCreate(Bundle savedInstanceState)...


android - How can I implement a ListView without ListActivity? (use only Activity)

I'm new to Android, and I really need to do it this way (I've considered doing it in another Activity), but can anyone show me a simple code (just the onCreate() method) that can do Listview without ListActivity? THanks


android - add buttons to ListActivity

How do you include add, remove, and back buttons in ListActivity .pl?


Android: Adding static header to the top of a ListActivity

Currently I have a class that is extending the ListActivity class. I need to be able to add a few static buttons above the list that are always visible. I've attempted to grab the ListView using getListView() from within the class. Then I used addHeaderView(View) to add a small layout to the top of the screen. Header.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android...


android - ListActivity Item with remote image

I have a ListActivity that should display quite a lot of items and where each list item should contain a text and and an image. The images are gotten from a remote server. How can I display the remote image on the list item. Thanks in advance


Android: ListActivity design - changing the content of the List Adapter

I would like to write a rather simple content application which displays a list of textual items (along with a small pic). I have a standard menu in which each menu item represents a different category of textual items (news, sports, leisure etc.). Pressing a menu item will display a list of textual items of this category. Now, having a separate ListActivity for each category seems like an overkill (or does i...


android - First list-item selected when starting listactivity from EditText event

i made a small test app with 2 screens. the first screen contains an EditText and a Button. The second screen is just a simple ListActivity showing 3 static items. The ListActivity is started when the user clicks the Button on the first screen or when the enter key was hit in the TextEdit. The weird thing is if the ListActivity was started by pressing the enter key in the EditText view, then the first list item is selected...


android - Change tab using onClickItem in ListActivity

Can someone please tell me how to change tab by clicking on element INSIDE the tab? I already tried it with global data. The code looks like this: public class Tabs extends TabActivity { int tabNumber = 0; private TabHost tabHost; int returnedTabNumber = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ...


android - Putting SQLite data into ListActivity

I'm trying to create a list adapter that pulls in and displays data from a database. From what I can tell, it is pulling in the info, but it fails when I try to create the ListAdapter from the new Simple cursor adapter. Not sure what I'm doing wrong here. SQLiteDatabase db = myDbHelper.getReadableDatabase(); String select = "Select StateID, State, Details from States"; Cursor cursor = db.rawQuer...


android - ListActivity layout Custom

I'm trying to make a list where each row has a text at the top and bottom and another one image aligned right (like the image attached). but I can not. Could anyone help me? sample image: http://www.freeimagehosting.net/uploads/fb6e2055f5.png


Android: ListActivity with ListView start new intent and this intent don't have parent

I've a ListActivity with a ListView, onItemClick i start an Intent, Oncreate of this Intent i make a getParent but it's null. if i do this.isChild() it's false. mPostList.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Intent IntentDiscution = new Intent(parent.getContext(), EcrireMessage.class); ......






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top