sorry for may be stupid question, I am a beginner. what can I do if I needed to extend both ListActivity and FragmentActivity in one activity?
I know that I can extend only 1 class, but is there any trick which resolves this?
Thank in advance.
you don't have to extended list activity in order to use list , you can include list View inside your layout file
No, you cannot extend 2 classes in Java. Could you specify why you need to extend both? My guess is FragmentActivity should suffice for your case.
You cannot extend more than one class. You said you're beginner, whenever you dive deeper on how the objects work you'll see that it's simple impossible.
But, to answer the bigger picture of what you're probably trying to achieve:
You want to have one Class that will extend FragmentActivity, and a different class that will extend ListFragment. And whenever the FragmentActivity is created you'll instantiate one of your ListFragments and add it to the layout.
Related
I am extending ListActivity in one of my Activities but I need to extend one more Activity.
So, is there an alternative for extending ListView in Android?
I am using the ListView to display the data from database.
First of all: Java does not support multiple inheritance.
After this, what I would to, is to rethink in the design: does it make sense to your class to be ListActivity and other Activity?
If yes, there are a couple of workarounds you can try:
Aggregation: make a class that takes those two activities as fields.
Interfaces.
I'm designing a similar interface to MathStep pictured below but my main activity already extends
public class MainActivity extends Activity implements TextWatcher
and Java doesn't have multiple inheritance.
In this program, you can ViewPager between Basic, functions and extra tabs. My program is not using tabs but I am swiping between fragments of buttons in a RelativeView.
How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?
I have been working on this with a friend for, I'm not kidding, 8 hours before we resorted to asking a question here. I have looked at this and this post and they were very helpful in understanding how ViewPager and Fragments work, but in all the examples they reference full paged ViewPagers, every time we attempted to create this scenario, something either wouldn't compile, or we weren't linking our XML correctly and code wasn't running... I've searched extensively for this answer and I hope I'm not the only one who has struggled with this so others can learn.
I'll admit, part of the confusion has been the learning curve but that's why I'm doing this, to learn.
How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?
All Fragments have a reference to the Activity via the getActivity() method, but you should only use it if you really need a handle to the context.
Inheritance is not required in any way whatsoever (technicality: other than for your activity, fragment, and FragmentPagerAdapter which must inherit from their respective parent classes...). The ViewPager itself can be included in the view heirarchy by referencing it from XML. The different fragments are displayed in the ViewPager by a FragmentPagerAdapter that you will have to implement yourself, this should be a separate class. If you want, it can be an inner static class, but, do not use an inner class. Keeping the scope organized by forcing dependencies to be passed through constructors will keep your code clean.
You should start by reading the ViewPager/Fragment related documentation on d.android.com. There is example code for these things and once you understand them individually everything will come together.
I am using a class which extends Activity to obtain a list from an API...
In some apps which do basically the same, the devs extended ListActivity...
Which are the differences?
ListActivity extends the functionality of the common android.app.Activity by providing a number of list centric features 'for-free' if you like. For example, the handling of a list entry click is neatly contained within ListActivity's onListItemClick(...) whereas if you were using a plain android.app.Activity then you would need to implement this manually with an OnClickListener and implementation.
By all accounts, if your layout contains a list then use a ListActivity/ListFragment since it is a useful extension. It does not mean your whole screen layout has to be list but a portion of it has to host a ListView widget with identifier, id="#android:id/list".
The Javadoc on the class with useful examples of how to use it can be found here.
ListActivities are specially designed to be used with ListViews. It provides several helper methods like onListItemClick(), which make it easier to use a ListView in them.
You can do anything you can do in an Activity in a ListActivity.
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 (Jelly Bean code linked to) and see that it doesn't do all that much other than make your life a little easier.
Extending from the ListActivity you agree with the contract that in the layout of your activity a ListView component will be available.
Your ListView component should have the id: #android:id/list
The ListView class provides convenient methods for working and manipulating the ListView
Also, in a regular Activity you can use the code below in onCreate to hide the app titlebar. It seems that you can't do the same in a ListActivity. (learned this the hard way)
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_list);
// The rest of the content of onCreate
Newbie here, bear with me..
I have a class that extends TabActivity. One of my tabs currently contains one long string composed of some records i got from my sqlite table. Now i'm following a tutorial and the next step is to move away from the string and create something a bit more appropriate for the records, a listView. However it says i'll need to change my class to extend ListActivity but i've already extended TabActivity (not part of the tutorial)and i don't really have the knowledge yet to know where to go from here.
I figure if my tabs content was set with intents to separate classes I wouldn't have a problem but it's important to me that both the tabs and their content are constructed in the same class because i will want to be doing a lot between them later. But maybe there is no way i can avoid creating a separate class? If so could i still point both of my tabs towards the same class? for the same reason as above. But then how would i get them still displaying different content?
I sure there's an obvious solution but i don't have enough programming experience to have a clue what it is, I've been googling a while now but i'm not really sure what to look for so if somebody could point me in the right direction i'd really appreciate it.
Hope I'm talking sense, Here's a link to see the class i'm working on:
Codeviewer.org , my Budget.java
You don't need to have your class extend ListActivity (or TabActivity for that matter) as those classes are just a convenience.
If you want to show a ListView in a TabActivity, check out this tutorial I wrote.
You can always insert a ListView to your activity, give appropriate android:id so you may refer it to handle data. Theres no compulsion that a ListView must be in a ListActivity.
See my comments so I can help you more.
You don't mess with the TabActivity as this Activity just manages all of your tabs. You just set the new ListActivity as the content of the tab you want to display it in.
Check out the Android Hello Tab View tutorial as it shows you how to assign an Activity to a tab which is just what you need.
While programming on Android, I end up writing a parent activity which is extended by several others. A bit like ListActivity. My parent activity extends Activity. if I intend to use a Map or a List, I can't use my parent activity as superclass - the child activity can only extend one activity obviously. As such I end up writing my parent activities with the same logic for Activity, ListActivity, MapActivity and so forth.
What am I looking for is some sort of trait functionality/design pattern which would help in this case. Any suggestions?
I really dislike ListActivity, MapActivity etc. Basically they are activities which simply add a single view element at the cost of some flexibility. By adding a MapView or ListView to your XML appropriately, you end up with the same thing which can extend an Activity derived superclass directly. So just don't use any of those SomethingActivity classes for the most part.
I've ended up having a base MyAbstractActivity extends Activity that incorporates shared logic and a MyAbstractListActivity extends MyAbstractActivity that mimics ListActivity (inflates layout.R.id.list, layout.R.id.empty, etc.; not much going on there).
I'm using a delegate with all the shared functionality. This enables me to have all the shared functionality in one single class for all the different activities.
All my activities extend their special activity and then implement a common interface. The problem with this approach is that I need to implement all the methods defined in the interface and call the matching method in the delegate object. This code at the moment amounts to 30 duplicate lines of code and I think that is not that much of a problem.