populate listview with multiple datasources - android

Is it possible to populate a the same listview with 4 different sources?
at the moment my design is a
textview
listview
textview
listview
what i dont like is the two listview scroll indepently so if the top one is almost empty i have alot of empty space on my screen,
if I dont set the top one to a fixed size and it is very full, the bottom one is not shown.
I would like to have one listview/scrollview so everything is arranged nicely indepent on how many entries are in either of the arrays i use to populate my listviews.
can that be done?
thanks
this is my xml as mention above
<TextView
android:id="#+id/gamesView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/top_btns"
android:gravity="center"
android:text="#string/gamesView"
android:textColor="#FFFFFF"/>
<ListView
android:id="#+id/gameslist"
android:layout_width="match_parent"
android:layout_height="260dip"
android:layout_below="#id/gamesView" />
<TextView
android:id="#+id/finishedGamesView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/gameslist"
android:gravity="center"
android:text="#string/endedGamesView"
android:textColor="#FFFFFF"/>
<ListView
android:id="#+id/finishedgameslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/finishedGamesView" />

Related

Listview and TextView alignment

I have a listView which takes data from a rest API server and displays it. I need to add a textView on the side of it describing the type of data.
The right half of the screen has the listView and the left half has the textView.
The problem arises when the textView moves to accommodate the different screen sizes. I need the textView to be fixed and to be shown in conjunction with listView, and be in a straight line with the listView, regardless of the screen size. Cheers.
Edit: Image added of the sketch
It sounds like a ListView is not what you want to use. You cannot align Views with the children of a ListView. You would need to either put the TextViews within the ListView's children or not use a ListView.
The simplest way to do this would be a vertical LinearLayout containing many horizontal LinearLayouts.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"/>
<Item that was in the ListView />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"/>
<Item that was in the ListView />
</LinearLayout>
...
</LinearLayout>
The ListView is really for scrolling and when you don't know how many items there will be.
Perhaps you already have an adapter in which you are handling the creation of the items and that's why you are using a ListView. You can still create the LinearLayout's children programmatically by inflating each item individually and adding it to the container view.
Original Answer:
You probably want the TextViews to scroll with the ListView as well. So I would think you should use one ListView, then add the TextView to the left side of the ListView item.
You should use weightSum,
Here is an example.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="horizontal">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World"/>
</LinearLayout>

Display list of lists using mvxlistview

I want to display a list of lists My data is an ObservableCollection where the SomeClass has another ObservableCollection in it.
Here is the code:
This is the main list:
<Mvx.MvxListView
android:id="#+id/MenuList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="ItemsSource Path=Dishes"
local:MvxItemTemplate="#layout/menuitemtemplate"
android:background="#color/backgroundlightgray"
android:layout_weight="1" />
Here is the item template which has another list inside it
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Com.TasteITAndroidClient"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:gravity="right"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="#ff000000"
android:background="#color/white"
android:padding="10dp" />
<Mvx.MvxListView
android:id="#+id/CategoryDishes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource Path=Items"
local:MvxItemTemplate="#layout/userpagedishtemplate"
style="#style/ToolBarImage"
android:background="#color/backgroundlightgray"
android:layout_below="#+id/textView1"
android:layout_marginTop="5dp" />
The userpagedishtemplate displays the item.
Basically it works but I am getting very small sized lists which show 1-2 items although I specified height=wrap_content on the subList.
Have I missed something? should I be doing something else?
Thanks
Amit
Placing a ListView that scrolls vertically inside another ListView which scrolls vertically is likely to cause touch area confusion for your users - which list scrolls...
If you really want a list within a list then you cab use the layout you've started on - but you will probably need to give the inner lists some assistance with their vertical sizing - something more than wrap_content
Alternatively, for very small inner lists, you could consider switching to MvxLinearLayout.
Alternatively, you could consider making the inner lists scroll horizontally.
Alternatively, you could consider collapsing your lists into a single list using a polymorphic-aware adapter to determine whether to show a header cell or an inner cell.

Android: I have a fixed header for a listview but the "glow effect" is only for the listview

I'm trying to make a layout similar to the people app, so I have a list with a fixed header at the top. I define the header on top of the listview inside a relative layout but the "glow effect" at the top of the list is only for the listview and not the whole layout.
This is what I have:
and this is what I want:
This is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:minHeight="20dp"
android:orientation="vertical" >
<TextView
android:id="#+id/status_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/last_updated"
android:textSize="12sp"
android:textStyle="bold"
android:textAllCaps="true"
android:gravity="center|left"
android:layout_marginLeft="20dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:textColor="#color/nice_blue"
/>
<View style="#style/HeaderDivider"/>
</LinearLayout>
<ListView
android:id="#+id/status_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:layout_below="#+id/header_layout" >
</ListView>
The glow effect in both examples are functioning the same.
What is happening within the People application is that it is also a ListView just like yours. The effect happens in the same place. The Me, #, A, etc headers are actually inside the ListView.
Option 1
To do what you are looking to do, put a header in the ListView, you'll also need to create a custom ListView. This means extending ListView.
It sounds more challenging than it is, but you should be able to find a number of examples online. Look for examples that extend a ListView rather than creating a custom ListView.
The general idea is that you'll add in a TextView into the the View of the ListView that will act as your header. This will put the header text Last updated at inside of the ListView view that you've created and since it is inside, it will be included underneath the glow.
Option 2
You may be able to use the addHeaderView(View v, Object data, boolean isSelectable) or addHeaderView(View v) methods on the ListView itself.
I believe this will add a header row inside the ListView, but I've never used this so I can't say for sure how it works. This may be easier, but will be less flexible.

Android layout - listview and edittext below

I'm trying to mimic the behaviour of the HTC SMS application (tradional view), where all messages are shown, and an EditTextis shown below. As you can see in the screenshot, when scrolling upwards, the EditText scrolls away at the bottom.
I'm stuck with this, even after reading multiple posts (eg Android Layout with ListView and Buttons and this website: http://www.finalconcept.com.au/article/view/android-keeping-buttons-visible), it's not working as expected.
Thanks to the comments and EditText now showing under ListView, I've managed to have my ListView take all available space and start scrolling once completed. The EditText is showing at the bottom of the screen now - always. I'd like it to disappear at the bottom when I scroll up though - now it remains at the bottom
Current Code:
<?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="#android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TableLayout
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<EditText android:id="#+id/newmessagecontent"
android:layout_height="150dp"
android:singleLine="false"
android:gravity="top"
android:layout_width="250dp"
android:layout_alignParentTop="true"
/>
<Button android:layout_height="wrap_content"
android:id="#+id/sendmessage"
android:text="Send"
android:layout_width="wrap_content"
/>
</TableRow>
</TableLayout>
</LinearLayout>
i think what you need to implement here is some sort of modification of the SeparatedListAdapter from Jeff Sharkey from this Article. In this article he not only manages to add two Adapters to a ListView but also explains how to have Headers to separate them if you want (you can remove that part of the code).
So what i mean, is your first Adapter will be the data with It's rows, and the second Adapter will be a dummy one with no data that just points to a View with your controls or whatever.
this way the ListView and what you want to add at the bottom are gonna be all scrollable.
Hope this helps.
A ListView automatically scrolls if all the items in it take up more space than the view provides. What happens if you remove the ScrollView?

Why does Android listview sometimes disappear after calling requery on underlying cursor?

I have a fairly simple application that uses a ListActivity to display a ListView. Each ListView item has a CheckBox so that the user can specify favorite items. I am using a class that extends SimpleCursorAdapter so that I can capture both the CheckBox changed event (to save the change to the database) and the list item clicked event (to load an item details activity).
After the OnCheckChangedListener implementation updates the database, I call .requery() on the cursor so that when an item scrolls off and then back on to the screen, the CheckBox has the proper state.
That all works fine. Where I am having problems is in another activity where I am reusing the same ListView layout and the same cursor adapter to display a specific subset of items as part of a different activity's layout. When I use the layout there, everything is fine until I tap the CheckBox, at which point the entire ListView disappears. The database is updated, but until I leave the activity and come back, the ListView is just totally gone.
If I don't call .requery() on the cursor, everything works fine; the ListView does not disappear (but the CheckBox state will be wrong some of the time, as described above).
Working layout, event_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content" android:text="#+id/eventsHeader"
android:id="#+id/eventsHeader" android:gravity="center_vertical"
android:layout_height="wrap_content" android:textSize="15sp" />
<ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/showHistoricalEvents" />
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/showHistoricalEvents"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:text="Show Past Events" android:textSize="14sp" />
<TextView android:id="#+id/android:empty" android:text="#string/no_events"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/showHistoricalEvents" />
</RelativeLayout>
Broken layout, track_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content" android:id="#+id/InterestLabel"
android:layout_alignParentLeft="true" android:text="#string/interest_level"
android:gravity="center_vertical" android:layout_height="fill_parent"
android:layout_above="#+id/track_name" />
<Spinner android:layout_height="wrap_content" android:id="#+id/InterestSpinner"
android:entries="#array/priority" android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/FavoriteCheckbox" android:layout_width="fill_parent"
android:layout_toRightOf="#+id/InterestLabel" />
<CheckBox android:layout_width="wrap_content" style="?android:attr/starStyle"
android:id="#+id/FavoriteCheckbox" android:layout_alignParentTop="true"
android:layout_alignParentRight="true" android:gravity="center_vertical"
android:layout_height="fill_parent" android:layout_above="#+id/track_name" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/track_name"
android:text="#+id/track_name" android:textSize="25sp"
android:layout_below="#+id/InterestSpinner" />
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_below="#+id/track_name" android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView android:text="#+id/track_description" android:id="#+id/track_description"
android:scrollbars="vertical" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:paddingLeft="5dp" android:paddingTop="5dp" />
<include layout="#layout/event_list" android:id="#+id/trackEventList"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="2.5" />
</LinearLayout>
</RelativeLayout>
My only thought right now is that the way I am using layout_weight in the second layout is causing some weirdness. I am loath to change that, however, since it took me quite some time to get the layout to work the way I wanted it to in the first place.
One other note, when I am including the first layout in the second, I am hiding the TextView and CheckBox controls because they are unnecessary.
Any ideas what I am doing wrong here?
Where I am having problems is in
another activity where I am reusing
the same ListView layout and the same
cursor adapter to display a specific
subset of items as part of a different
activity's layout.
If you mean the same instance of SimpleCursorAdapter, that is so not a good idea. Please make a new adapter.
When I use the layout there,
everything is fine until I tap the
CheckBox, at which point the entire
ListView disappears. The database is
updated, but until I leave the
activity and come back, the ListView
is just totally gone.
Use hierarchyviewer to determine if the ListView has no children (rows), has no height, is invisible/gone from a visibility standpoint, or is truly removed from the view hierarchy. That will give you better ideas of how to proceed.
Any ideas what I am doing wrong here?
Besides the adapter problem mentioned above, you're using CheckBox. Android has built-in support for multiple-select lists (e.g., android:choiceMode="multiple"), but that needs a CheckedTextView. I would aim to use Android's built-in multiple-selection logic rather than rolling your own.

Categories

Resources