3 Level ListView in Android - android

My situation is as follow:
I want to know if it is possible to have an (Custom) ListView with an linear layout in it, where in the Linear layout another ListView. What I want to accomplish is an List of object with in it an list of object with an list of object. That means that the list goes 3 category(level) down. Company have several Establishments in it and Establishment have several Departments in it. I will try to sketch it.
[ Company] //ListView
[Layout]
[ Establishment] //ListView
[Layout]
[Department]//ListView
[Department]
[/Layout]
[/ Establishment]
[/Layout]
[/ Company]
I have a main class and an Adapter class for every Object (Company, Establishment and Departments).
I know how to make an ListView and retrieve data to display it in the ListView. But I don't know yet how to use it when you have to go down in hierarchy of another ListView or when you have ListViews that relates to each other. Maybe I should use an Expandable ListView(But I don't want the collapse/expand behavior) It have to look like a ListView, maybe an Linear layout that behave like an ListView. Or use section headers to present the data as I want to.
Well on my question I ended up using an ExpandableListView with an GridView.

Related

Items with dynamic content in a listview

I'm pretty new to Android and I have a question about an application that I'm trying to develop.
My problem is about listview and adapter: I have a list of objects that contain information about a user's favorite routes (i created an object FavouriteRoute and i pass an ArrayList to the BaseAdapter). However, each object can contain a variable number of cities, of stops and days of the week ... how do I handle this dynamic content and show it inside of each row of listview? What's the best approach? I have to manage these elements within the getView () of the adapter and insert them dynamically within the layout of each row?
You can take the reference of these tutorials to learn creating your adapter which will server your purpose.
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html
http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
If you want that some of your views are visible and some are not then in getView() do this:
if(your condition)
textView.setVisibility(View.GONE);
after inflating the views.

Android display multiple lists values

I am doing android application. In That I want to display a List of podcast urls like shown in the Image. In this I also share this url into FB, twitter and etc and also the user clicks the arrow symbol I want to forward to that podcast url.
I am having those values in separate arraylist (i.e. "4353,3424" as a arraylist and "567567, 234234" as a another arraylist likewise). So how can I display these values like shown in the attached image. Can anyone help me how to proceed to display like this?
I'd recommend you had a look at some of the tutorials on how to implement your own custom ListView.
An example can be found here: http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html
Also I'd recommend you create a custom class for holding the different informations in every row of the ListView. When doing it like this you can have one single List<MyCustomObject> holding all informations and then when a row is clicked, you simple get the item from the List<MyCustomObject> and get the specific property of the custom object and act accordingly.
EDIT: Small example of how to add onClickListener to a sub-view of the row:
Inside the getView method of the custom Adapter you can use setOnClickListener to the views, you'd like to respond to clicks. For instance:
myImageView.setOnClickListener(this);
Then let your custom Adapter implement the interface OnClickListener and act accordingly to the clicks.
Another way would also be to add a Share Intent to the different images, like in this example:
http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent
You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter

Complex Items in a Listview

I have a ListView where the view for each item is a string (the name of the item). But I have to associate a lot of other data with that item: price, size, weight, tax, etc. So, I'm of creating a new Java class called Item, and then an object for each item in the list.
I don't know which is the bext way to implement this. There's two obvious choices:
1) I can simply create the data structure outside of any Android Views, and then write a method called UpdateList() which takes the name of each item in this data structure and puts it in the ListView. The problem with this is that some of the data is duplicated twice (the original data structure, and the adapter for the ListView) and when you duplicate data, bug potential arises.
2) Or, I can somehow associate the data structure directly with the adapter for the ListView and have it figure out how to display the name for each ListView entry that is displayed. The advantage here is that you only have a single data structure. But I don't know if this is possible in Android, or very complex.
Which is the preferred way to do this with Android apps?
You would be better with the ListView and the Adapter option, You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Just use the adapter. It's much cleaner. Then you can retrieve the info you need when you display the list item with getView(). See this example.

How to list Views (or Widgets) in one scrollable screen

I have the following prototype:
What is the way to list views in a scroll-able way in a screen?
What I have already tried:
I tried to create a class and extends the LinearLayout class and dynamically adds my Views (the compound controls class is my view also extends LinearLayout) to the main LinearLayout. So I got a list of my views which is scroll-able.
I'm not sure what you mean, but looking at what you have tried, what's wrong with using a ListView? As the name implies, it should do exactly what you want:
What is the way to list views in a scroll-able way in a screen?
Here is a quote from the android documentation of a ListView
ListView is a view group that displays a list of scrollable items. The
list items are automatically inserted to the list using an Adapter
that pulls content from a source such as an array or database query
and converts each item result into a view that's placed into the list.
You should be able to add a ListView directly as a sub-view under your LinearLayout.
your questions seems like hard to understand.
did you mean how to set the whole page(activity) into a listview?
listviews are scrollable in default (of course, you can see it if the list is long enough and you can set the whole activity as a list).. you can customize it also by using templates (xml)
and extending ArrayAdapter of your type (let's say ArrayAdapter<UpcomingEvents>) and supply data from db...
is this what you mean?

easier way to make listview in android?

i have to make a listview in which there are two elements to be displayed vertically.
i know that to use the default adapter given with android there can only be one array and one text resource...ie if i am using android.R.layout.simple_list_view then there is only one text resource.
To make a custom Listview i am doing the following:
making a xml layout file for each element of the listview
extending a custom adapter class which extends the baseadapter
in the getview method of the custom adapter class i am inflating the view for each element and then returning with the info i want the listview element to have from an array which i have passed as a constructor to the custom adapter class.
this seems very tedious because there are several instances where i have to make listview where sometimes there are three text elements in each listview element and sometimes 2 text elements in each listview element.
is there an easier way to do the above.
thank you in advance.
With such simple layout, I'd suggest you to just use a LinearLayout and 2-3 TextViews (or any view you need, even an horizontal LinearLayout). Nothing will beat that simplicity. There's no need for a ListView in that case.
You could consider creating a generic, reusable ListView layout file that is loaded up with all the various elements you need (which, hopefully is a concise few). You could default as many of those elements in the layout XML file with android:visible="false" and then programmatically toggle the visibility.
Why can't you just reuse the adapter? It's got plenty of loading/unloading methods associated with it.
Yes, what Aleadam is saying; if you only have a couple of things why use a ListView? TextView would seem a much quicker way to prototype data display!

Categories

Resources