Android same layout different data - android

I'm creating a local android application and am trying the following:
The main menu consists of 4 buttons, each will lead to a (different) list of chapters.
The layout of the chapters view is the same, so I am trying to use one layout for every chapters page, by using different strings depending on the button that's being pressed at the main menu. So when the user presses button 1, the list of chapters differs from when the user presses button 2, etc.
I have tried looking up solutions, but was unable to find any so far. Is there a way to call a certain set of strings depending on the button that is chosen?

Have you tried to use a custom list adapter which take your array of strings as a parameter? Your list of chapter is just a simple listview with one string in each row right?

Related

dynamically create a new activity by pressing of different buttons in different activities

my problem goes like this-
this is a commercial application in which users can purchase different products they see within app..
there is one view cart button available on the main screen
there are 10 different products available in an activity and there are 3 such activities( each of them containing various different products)
these products are arranged in a linear fashion (linear layout).
every product is having an add to cart image button.
what i want is, when the user presses that button, 3 things should happen..
a new activity will be created containing that product information (when the user presses another "add to cart" button, this new product will come below the previous product and so on).
there is one cart button in the menu bar, that cart should show a number equal to the nos of product in the cart.
its simple, a small flash notification that the product has been added to the cart.
any help will be deeply appreciated.
You would see some benefit by using Fragments instead. Use a main Activity as a "switchboard" and multiple Fragment instances to show the per item details.
You don't need to dynamically create a new Activity. Taking into consideration that you call yourself a newbie in android development, I can only tell you what to study to accomplish your plan.
First, learn to make an Activity that will contain a ListView. Make an XML layout for your ListView items, containing TextViews, Buttons, whatever you want. Then, make an adapter that will fill items with corresponding values (texts, button names, images, etc).
And second, make them all clickable (onItemClickListener or an onClickListener for every button) and make your Activity get inflated with a Fragment containing TextViews, ImageViews, etc... whatever you need to represent information about the item selected.
The best practical way is to make an empty Activity with an empty Fragment. The last one will be either inflated (populated) or even replaced with whatever things you need.
Sorry, but teaching you how to make all these things technically is beyond the scope of any sensible answer that someone can post here. It's all the matter of study and practice. But I hope the vector of study I gave you is transparent and affordable.

Which is the better method in Android for creating a dynamic list?

If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...

How to structure an app with many similar views/activities?

Background:
I'm creating an Android app (my second) and I'm not really sure how to structure it.
The section of the app I'm having issues with is basically a sort of simple catalogue; a user clicks a button which opens a layout containing a title (TextView) and an image (ImageView).
Image for clarity:
What is the best way to structure this? Since I'm pretty new to Android development, my approach would have been to create an activity for the first view (no.1), create a seperate activity for each of the "Products"-pages, "Products 1", "Products 2" etc. and a third activity for the single item view.
I know how to reuse the "single item"-layout (no.3) but I'm not really sure what the best approach is for no.2.
Question:
Should I create six new activities for "Products 1", "Products 2",..,"Products 6" and corresponding layouts for each? Or should I try to keep the number of activities to a minimum?
Any help is highly appriciated.
Thanks!
Your app is like a tree-structure app, so three activities/views are enough for your application.
Products Category List(Contains Product1, Product2, etc.)
Product List(Contains Product1-1, Product1-2, etc.)
Product(Contains image for a product)
You should not create an extra activity for every product because it will consume too many system resources.
For coding, you can make a base class since these three activities are similar, and put common elements into the base class.
If you use listviews the first two screens can be just one activity, with different data passed in to display screen 2. And screen 3 will be common for all, another activity.
If you are planning on keeping the screens as you displayed above, with buttons, then you can just create one layout with button, use it as a row layout within a listview. The activity logic would be such that, depending on data, it would populate one or more rows. With this option too you can do screens 1 and 2 using one activity and one set of layouts.
You make one layout:-
1. layout one you can visible(visibility) first 6 textview and other will b gone(visibility).
2. Layout two you can visible(visibility) textview what you want and other will b gone(visibility).
3. Layout three you can visible(visibility) only image view and other will b gone(visibility).
note:- change textview value through setText.

Simple List & List using Database Values

i need some suggestions first,
My application is like, on main screen a user clicks the image button "Courses" and navigates to a list, with three items, when user touches any item, he moves into another "List view" that pick up values from the database, and then if a user clicks on any item of second list,will be navigated to final view or a webview..
suggestion i need is, is it a nice practice to navigate a user from list to another list, in a
application..
and in Iphone application they make back button to each view,(as they dont have a back button on iphone) but in many android applications i didn;t seen back button in list view layout, do i need to have a back button on the top of the list view ?
Second question i have is, do i really have to make a Database for a list,
i have to make atleast 6 or 7 Lists in my application, with some list having values nearly 16
Ad. 1: No, you don't need back button in list view. It's real back button in all android devices.
Ad. 2: No. But when data are changing or you want to perform complex queries then database is preferred. In case of not changable data use xml. In case of simple data you can even use shared preferences.
Here is short guide to android data storage:
http://developer.android.com/guide/topics/data/data-storage.html
Here is simple tutorial to use hardcoded data with listview:
http://developer.android.com/resources/tutorials/views/hello-listview.html
I have a few comments on these:
1) The application might end up having a complex navigation to achieve simple tasks if there are multiple levels of lists. Consider using a Tab layout instead of the first level list, since you mentioned there are only 3 entries in the first list. Also try to consolidate your menus so that user can view options upfront
2) Back button: IMHO in android you should only implement it iff you want to override the default behaviour of the back button.
3) Database for a list: Totally depends on whether you want to change the lists once the app is installed. If they are static then you dont need to build it from the database

What is the best way to do multiple listviews in android?

i am writing a software that i have to drill down on content a lot. For example when the program starts a listview is displayed. When user clicks on an item, then a second listview must be displayed. For example:
Select Continent > Select Country > Select State > Select City > Select Address
What is the best way to do this (less memory, faster, easier to code etc)? To create multiple listviews with multiple adapters? Or 1 listview with multiple Adapters? Lists are loaded from an external XML File.
So far i am creating a new adapter and setting it to the listview. How do i create a second listview and after clicking on 1st listview displaying the second one, with animation. Any examples?
Extend my class to ListActivity or Activity?
I would make multiple Activities for this. The first activity to display Continent list, the second the Country list, Third State list, etc...
When the user than click the back button. It will go back to the previous activity (list). Even the scrollstate would be remembered.
This will also add the OS animation between the activities. The code will also be separated and memory freed when closing the list activity.
Sending a value from one activity to another ex CountryCode tot the StateListActivty is done by setting a intent.putExtra("CountryCode", countryCode);
A second approach would be to use a ViewFlipper. Adding each listview as a child. And than setting a custom animation on the show-next and show-previous actions.
Note: Using multiple activities may use abit more memory than the ViewFlipper approach.
I would use separate activities for each list but with only one List Adapter class to be shared by all so you can retain consistency in terms of how the lists look + easy to maintain code. You can use a bundle to pass info from one activity to another.
Another thought: is the info you are referring to part of "settings" info? By that, I mean, is it info the user would put in once into your app, or would they put it in almost everytime they use the app (because each time the info would be different)? If it's an "one-off" type of info, you would be better off using a PreferenceActivity.

Categories

Resources