TextView and ListView in a LinearLayout - android

I searched for similar questions but didn't get the proper answer, so posting it as a new question.
Can a LinearLayout have two TextViews and one list view? I learn that listview can be used to display array elements only if the class extends ListActivity. The Class in my app extends Activity and uses listView.
I am trying to build an RSS Reader based on IBM tutorial. On running the project I am getting parsed text in both the Text views but ListView is not displaying. I can post the code and Logcat if required.

Linear Layout can have any number of children.
ListActivity is an Activity that Android have added for convenience when dealing with activities comprised of list.
You can use ListView on a regular Activity and just implement an adapter that will populate the list items with data from your model.
Android has some ready-made adapters that can be used for simple use-cases.
of course if you need a more complicated beahviour you can extend them.
Have a look on BaseAdapter, ArrayAdapter and CursorAdapter to get a better understanding of how to use adapters.

You can have a ListView in any activity, using a ListActivity just makes your life easier (usually). See here for details.

You can have a Linear layout with different views and list view inside for example:
?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"
android:background="#drawable/background_new_search_activity_1">
<TextView
android:id="#+id/list_item_amount_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"/>
</LinearLayout>
And yes you can use the ListViewin all the activities, it is not necessary to extend ListActivity

Make sure the orientation of the ListView is set to vertical. Otherwise, it will try to display all items side by side, and those that fall outside the available view area won't be visible.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <!-- << orientation setting here -->

Related

How to add multiple rows of one linear layout into a single parent layout dynamically in program

I have a row.xml file which define a row whose content and number of rows will be dynamic.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/room_detail_row_in_hotel_detail">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/sz_blue"
android:id="#+id/price"/>
</LinearLayout>
The best solution as suggested by #Hell is to use a Listview.
Say you are operating within a fragment, add a listview in the xml for the same.
The fragment (or activity) containing the listview will dynamically create list(preferrable)/array of data.
In your case the dataset will essentially either be a list of class object which uniquely have two objects, price and name.
Create an adapter, could be a BaseAdapter or any other form depending on your specific use-case. This adapter would essentially inflate your row layout (as specified in your question) and dynamically add the data.
Add the data into the adapter using it's constructor. Following which assign this adapter as the listview's adapter and call the "notify" method for the adapter indicating that a new dataset has been loaded and the list needs to be refreshed.
Also note, it's highly suggestible to add smoothScroll to your listview for obvious reasons.
I guess what you are looking for is a RecyclerView.
You should use the Viewholder-Pattern to reuse your layouts

can i have a listview and an expandablelistview in the same view

I have several edittexts in my view for taking input for a database query .I'm displaying the query results in a list view below the edittextfields. Because there are 7 edittextfields there is not enough space for the listview. I wanted to put all the edittexts in an expandablelistview.
Problem i'm facing is -only the expandablelistview item is shown -the listview doesnt come up at all --i have #android:id/empty and #android:id/list entries ---but nothing comes up
i can't add the listview into the expandablelistview as that is not allowed + since i need both #android:id/empty and #android:id/list to show query results i need a proper list view.
what are my options --? i could ofcourse display the results in a separate window
If you could show the xml you have right now it would be great but the ideas behind it needs to be something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Edit:
Okay so i figured out that for this layout to function correctly you need to set dome weight so that the linearlayout understand what size to give each children. Use:
android:layout_weight="1"
in the listview and the expandablelistview (1 in both means thay will divide space between them)

Android - Creating a List like the Grocery IQ List

(Using Android version 4.0.4)
I'm trying to build functionality very similar to the list found on the GroceryIQ app:
I can create a nice looking list using the ExpandableListView shown in the xml below and a custom class that extends BaseExpandableListAdapter, but I'm having trouble with the second list. Is it two lists with a view in between? Could this be just one list with a special list divider that just looks like a bar?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ExpandableListView
android:id="#+id/ExpList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#null"
android:dividerHeight="5dp"
android:groupIndicator="#null"
android:paddingBottom="60dp" />
<include
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/bottom_bar_list" />
</RelativeLayout>
I've tried adding another list, but two lists on one view don't seem to cooperate too well-- either the first list is chopped off or the second list doesn't show.
Thanks for your thoughts!
So, I don't know that it's possible to know exactly how Grocery IQ crafted their list, but I found that I could replicate a pretty similar list by super-customizing the ExpandableListView.
An ExpandableListView is implemented using custom Group Items and Child Items.
I added a relative layout to the Group Item that looked like the "awesome bar". Then programmatically when I populated my list, I had a "rogue" Group Item that when I found it, I would show the "awesome bar" instead of the regular group-- in all the other cases the relative layout was set to invisible.
It took a while to get it "just right" and my solution seems a little like a hack, but that's the gist of what worked for me.

Android: what is the best way to show a TextView in case if no item in a ListView?

I need to show a list of items, the items are read from a database, and it is possible there is no item, in this case, I just want to show a TextView saying "there is no item", I think I could implement this by using relative layout, both list and text are in center of parent, they are displayed alternatively, but is there any way better than this?
Thanks!
Adding to Aleadam , Bill Mote
You may call at any time AdapterView.setEmptyView(View v) on an AdapterView object to attach a view you would like to use as the empty view.
The snippet is as follows:
empty = (TextView)findViewById(R.id.empty1);
list = (ListView)findViewById(R.id.list1);
list.setEmptyView(empty);
Make sure you keep the ListView and TextView inside the same parent.
For detailed description please refer this
If you're using a ListActivity, that is the default behavior. If not, then you can set the ListView visibility to GONE and a TextView visibility to VISIBLE.
Aleadam is right. Here's an example XML in support of his answer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#drawable/red"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:text="#string/error_no_groups"
/>
</LinearLayout>
The first tutorial on the Android developer website explains how to do this:
http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
(Look under Step 4)
Snippet:
The ListView and TextView can be thought as two alternative views, only one of which will be displayed at once. ListView will be used when there are notes to be shown, while the TextView (which has a default value of "No Notes Yet!" defined as a string resource in res/values/strings.xml) will be displayed if there aren't any notes to display.
The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.

Set, get text and value in listitem view in Android

I have listview collection that show id and name in Android. I following example from this resource site.
But actually that example only display name. I wish similar like checkboxlist in ASP.NET that contain property text and value. So the text to show name and value store id. When user select one item, I could retrieve id by viewtext.
My question is how do I set id and name in same viewtext item? I wish could pass the id to another layout.
I know that possible using multi viewtext then set invisible of viewtext using map. How that could be waste memory.
i would suggest a custom listview. you can google for "custom listview" and will find a bunch of tutorials on that. simply said, it is a listview that has a custom row that you can specify in a xml file.
here is a quite useful tutorial to start with.
here is what i coded, maybe it will help you. note that this is a bit different, because i am not defining a listactivity but a listview widget. but concerning the custom row item, it won't matter.
this will create a listview and specify an adapter for that listview.
CustomListView listview = (CustomListView) findViewById(R.id.list);
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String(getApplicationContext(), R.layout.rowoflistview, R.id.label);
listview.setAdapter(arrayadapter);
my rowoflistview.xml looks like the following. it adds an image and a text to each row of the list. you can of course change it to (mostly) whatever you want.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/rowselector"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/musicicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/musicicon"
android:paddingLeft="3px"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false"
/>
</LinearLayout>
as i wanted the listview as a widget in my main activity and not as a fullscreen activity, i had to do it other than you when it comes to event listening and click listening. if you want it a bit easier, be sure to extend the listactivity in your custom listview-activity and override the default methods.
hope that was understandable to get a grip on the topic ;)
Views have an associated tag Object map, which could contain your id http://developer.android.com/reference/android/view/View.html#getTag%28int%29

Categories

Resources