Maybe it is difficult to understand the title but let me explain. So I am working on an sports application. The main idea - I will parse official website which contains match schedule, results, stats, team info etc. Now I am working on the UI before I make functions for parsing and storage of data. It should look something like this:
ActivityTeams will contain the following buttons:
Team 1
Team 2
Team 3
...
For every team make a new activity but inside of these activities I will need the same buttons - Team info, stats, results etc. So it will be appropriate to make these activities only for once and just somehow load different data on them. If the path will be Teams > Team 1> Stats the data on display will be different than if it will be Teams > Team 2> Stats. How can I do this? I understand that I can open the same activity from every other activity I would like to but how to make the function for displaying different data in the same activity depending on the path? Any tips, suggestions? Thanks!
You can achieve this using intents, so go read the android doc.
Android Intents - Tutorial
You can use Intents in Android to switch from one Activity to other and pass some identifier(like Team name or Team id).
Call a Activity on button click and pass the identifier as an Extra in Intent.
Access the Intent Extra you passed in your Activity(which would start on click), and manipulate it with switch
Something like this
//In TeamActivity
Intent i = new Intent(getBaseContext(), TeamActivity.class);
i.putExtra("team_id", 01);
startActivity(i);
//In clicked Activity
int teamId = getIntent().getIntExtra("team_id",00)
switch(teamId){
case 01:
//Make changes for when Team with id 01 is clicked
break;
case 02:
//Make changes for when Team with id 02 is clicked
break;
}
Related
I am going to work on an android application.
The basic flow should be like :
1. Main Activity
**-cat1**
-subcategory 1
-subcategory 2
**-cat2**
-subcategory 1
-subcategory 2
**-cat3**
-subcategory 1
-subcategory 2
this should be the flow of application. Every Page contains static data only.
If we consider subcategory under category, it may contain some images and some data.
All Pages would be of same scene.
So, I just want to know what would be the best way to do so??
Can I just have to use resources only or database would come into picture?
Thanks.
Let Me add that I am clear with the flow my confusion resides into data storing only that what should be the best way to display static data only.
Editing again and again because I am not satisfied.
For data taking scrollable textview would be convenient?? I will put whole data in String.xml
Different Tabs for Categories and activity for your subcategory page would be better in my opinion. You need not use any database since most of the data is static.
I am new to Android App development, working on an android app which populate a list of numbers, in a listview dynamically, depending on the choice of the user, but, the moment user closes the App, the items in the listview are lost. How can I maintain the state of the listview?
Examples with code would be highly appreciated.
When I open Activity A, it allows users to add friends, and this friend list is shown in the form of items of listview in the same Activity, however, when I move to Activity B, and then come back to Activity A, this friend list disappears. I need to make sure that this friend list should not be lost while moving between activities. Please help.
I think that for your purpose there are 3 main methods, i'll explain them from the easier to the most difficult (in my opinion).
Text File
A way to do this is to create two methods in a class:
one has to create the text file in the storage if it isn't created before and read that, the other has to append a String to a StringBuilder and write it on the previous text file.
For this method you need the uses-permission of reading and writing to storage.
This link can help you: http://developer.android.com/training/basics/data-storage/files.html
JSON (also XML)
With JSON file you can create a list of objects with your data that you can serialize when you update the list and deserialize when you want to read it. For this purpose you have to study JavaScript syntax or, at least, JSON one.
SQLite Database
Android SDK incorporate a class named SQLiteOpenHelper that you can extend to create a database inside your app.
This link can help you: http://developer.android.com/training/basics/data-storage/databases.html
There are also references saving methods but i think that aren't right for your purpose, they work betters to save something like preferences or single data like last login informations.
I went through your comment. I would personally suggest using SQLiteOpenHelper
You might need to understand the use of SQLite, hence the tutorial
Simple Flow. On your Activity 1 where person Add Friends save it to DB
Then refresh the List from the DB. So when you move to Activity 2 and come back again to Activity 1 your List will refresh from DB. Hence no loss of data as you want.
EDIT
As user wanted to know how to use the ListView with DB.
Following are my suggestion
Sai Geetha
Youtube
I am quite new to Android
I studied how to use category (in the intent-filter). And I know category is used for grouping the activities. But didn't understand what is the real use of - category.
Can any help me in this regard - siting the practical use of category.
Thanks
The category gives additional information about the action to execute. For example, CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.
Here is a full list of categories with a short description
I want to develop an e buy application. I have categories and sub-products and data is kept in external server. If I add a new category on the server I want to add category and product activity automatically. Is it possible? How can I create a new activity automatically and how can I add the activity automatically in the android manifest?
No, you cannot do this. Activity objects must be declared at compile-time and cannot be changed later. You must adapt your code to work in the different situations.
For example, let's say you are fetching a name, birthdate, and description for a human resources entry. You would create one Activity that contained your layout and text fields, then have that Activity fetch the name, birthdate, and description from your server. It would then populate the text fields with the data, or show/hide the fields if there is no data for them.
This is a design issue, the activity should not be binded with your data. You should design an activity for categories data and an activity for products data. If they have different look, just build different sub class of your root activities.
My code is as follows:
First, I was wondering about line 20:
I had two questions:
a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE?
b. What is com.example.myfirstapp.MESSAGE?
c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?
Secondly, about line 40: intent.putExtra(EXTRA_MESSAGE, message);
I am not sure if this method adds a message to the upcoming activity to be called or what... Partly, I am struggling to understand this due to not knowing the point of an Intent fully.
I want to read my 200 fundamental section on what everything is, but I have set deadlines and I have been told not to take that approach for the time being for this project
With given the explanation of the Android Docs , I know an intent is:
The Intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed
A.) Could someone explain what the intent is used for or give some better quick articles than just the docs?
B.) Explain what putExtra( ) does and and these parameters more clearly:
name The name of the extra data, with package prefix.
value The String array data value
An Intent is appropriately named; it's what you want to be done. As the documentation says:
Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
By your code, you are familiar with starting an Activity via Intent:
new Intent(this, DisplayMessageActivity.class);
This uses your current Activity as the context from which to start the Intent, and gives the target class to launch. You already know this, I think. Basically, the Intent is just a guide for the Android device to follow so that it launches the right target with the right information.
Onto your real questions:
"What is the intent used for?" This is described above; basically, it's used to tell the OS what your target is, where it's coming from, and what data it should provide. You've seen most of this in action without realizing; this constructor is the one you've been using, detailing the "from" and "to" portions. When you use putExtra, you are providing the Intent with data it can give to the "to" part of the code.
The name parameter is best summed up by the documentation: "The name of the extra data, with package prefix." This is like a key in a HashMap; it is a string identifier of the content you are packaging. They tell you to use your package's prefix, just to prevent confusion. In your case, you should be using "com.SG.Three_Piece_Radio.YOURKEYNAME"; this does not have to be declared anywhere, nor is it a constant. Just a string. The value is just the contents of the extra (the data); this can be a ton of different things--short, int, String, Parcelable, and many more. (These can all be found in the various putExtras in the Intent docs.)
Once your Intent is received, you can use those same bits of data (for example, String myStr = getIntent().getStringExtra("com.SG.Three_Piece_Radio.YOURKEYNAME");) and do whatever you wish with them in the Activity you called.
I think people have been very helpful here in giving great explanations about Intent itself and its purpose. I got to learn a lot from these answers.
However, there was a small aspect that I think needs a little more explanation.
So to answer your very first question that says :-
a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE? b. What is com.example.myfirstapp.MESSAGE? c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?
My answer would be :-
So as all explained, putExtra is meant for carrying additional information/data along with the intent for the new activity that is going to be started. This additional information that putExtra carries is given in Intent in the form of a Key-Value pair.
In this Key-Value pair, the Key syntactically always has to be a String.
In your case, the value is also a String and the "key" can be any random string.
Now, to make sure that the system does not confuse your KEY with some other app's KEY you should always append the entire packet structure of the string along with it. And hence you use :-
com.example.myfirstapp.MESSAGE
where MESSAGE is actually the name of the key, (the String as is required, as I mentioned above) that would be associated with the string value that would be passed with the intent to the new activity.
Now you could have very well written the following as well :-
intent.putExtra("com.example.myfirstapp.MESSAGE", message);
instead of :-
intent.putExtra(EXTRA_MESSAGE, message);
but then this would reduce the flexibility of your code for changes to be made later. As for any changes in the key name you will have to change it everywhere. So to avoid this we rather assign the name of our key (in your case, MESSAGE) to a String variable (in your case EXTRA_MESSAGE).
This also makes it easier for other activities to reference this key by a simple String variable. And so to make it accessible to other activities (coupled with other self explained features)you make it as :-
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Please correct me if I happened to miss something or went wrong somewhere.
The most common use of intents is to start new activities (screens)within an application (line 41). The extras Bundle is a way of passing data between activities. Extras are entered as key value pairs so EXTRA_MESSAGE is a key is used to identify a particular value so it can be retrieved and used by another activity.