I'm using a custom SimpleCursorAdapter. Currently my list stays empty, if no entries can be found in the database/cursor. Now, I want to display a message within the list, if there are no entries within the cursor/database. How can I handle this event?
Just place the view you want displayed in your XML, and give it any id you like, eg:
<ListView
android:id="#+id/myList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- view to be shown/hidden on empty list above -->
<TextView
android:id="#+id/emptyListElem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Nothing to show!" />
Then in your Activity's onCreate() method, call setEmptyView() on your listView object. The ListView should handle showing/hiding your view object, depending on whether the list is empty or not.
View empty = findViewById(R.id.emptyListElem);
listView.setEmptyView(empty);
If your ListView is expressed in xml, you can add a TextView with a specific android:id which will automatically display your message if the list is empty, as below:
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" />
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:text="No Data exist"
android:textSize="16dip"
android:textColor="#color/black"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
You must use the #id/android:empty for this to work.
Related
I am creating an App, in which I am showing information from a database in a ListView, using a custom row layout, which is populated using a SimpleCursorAdapter. I also handle onItemClickListner for the ListView.
My Layout of the custom rows is as follows:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="true"
android:padding="10dip" >
<ImageView android:id="#+id/imageButtonAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:src="#drawable/icon_account" />
<TextView
android:id="#+id/TextViewAccount"
style="#style/textView_normal_bold_style"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="#dimen/text_size_large" />
</LinearLayout>
But I have one issue, which follows:
When I click above content of ListView row, it performs only click event, but what I want is when I click any where in a row then a click action should be performed. But it does not behave like this.
The XML of your row should have the attribute
android:layout_width="fill_parent"
I would like a custom ListView containing a set of queues that a user can subscribe or unsubscribe to. Each item in my listview should consist of a RelativeLayout containing a TextView and a Switch. How do I go about populating this list ?
I have an array of JSON Objects where each JSON Object has a name and a string value(whether this user is subscribed to the queue or not. So :
qInfo[i].optString("name") should set the Text of my TextView.
qInfo[i].optString("value") should set the status of my Switch.
queues_item :
<?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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/queuesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/queues_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Switch
android:id="#+id/queues_item_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textOn="Tilmeldt"
android:textOff="Frameldt"/>
</RelativeLayout>
You can just create a custom adapter and then inflate your view and return it with the correct data entered in. There is a link below for an example, the main part is implementing the getView() method.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
I have a ListView that inflate for each row a xml that contain a CheckBox and more TextViews that are in a RelativeLayout.
The problem is that I can't figure out how to pass onClick events from checkbox to back row.
I want to achieve this behavior: The user press the checkbox and the whole list row gets pressed. I saw this behavior if I inflate for each row android.R.layout.simple_list_item_multiple_choice but I can't figure out how to do that without this android specific layout.
Can anybody give me an idea or direction?
Below is the code:
Listview :
<ListView
android:id="#+id/include_sent_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:cacheColorHint="#color/white"
android:layout_alignParentTop="true"/>
And the xml that is inflated for each row:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/checked_radio_button"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:focusable="false"
/>
<TextView
android:id="#+id/account_number_text"
android:layout_width="100dp"
android:layout_height="fill_parent"
style="#style/config_simple_small_text_view_style"
android:paddingTop="15dp"
android:layout_toRightOf="#id/checked_radio_button"
/>
<TextView
android:id="#+id/account_name_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/config_simple_small_text_view_style"
android:paddingTop="15dp"
android:layout_toRightOf="#id/account_number_text"/>
</RelativeLayout>
The Checkbox consumes focus for the List item. You need to set this in your layout file:
<!-- Must make this non-focusable otherwise it consumes -->
<!-- events intended for the list item (i.e. long press) -->
<CheckBox
android:id="#+id/item_entry_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
/>
I have this ListView whose items i'd like to hide depending on the selection of a RadioGroup. Currently I'm passing a boolean to the ListAdapter due to the RadioGroup having only two options. My items contain a checkbox and i want to either show the entire list or just the ones with the check boxes checked. I'm succeeding at hiding the items but the dividers still show, how can i fix this?
Look how it looks like
http://www.mediafire.com/i/?wa2s0ngq027vjwr
http://www.mediafire.com/i/?9i6ggj2fdsns2da
(I'm new, so i can't upload images here)
The xml for my row would be:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="1dip" android:gravity="center_vertical"
android:background="#FFF">
<CheckBox android:id="#+id/dispositivo_tv"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="15dip"
android:layout_alignParentLeft="true" />
<LinearLayout android:id="#+id/botones"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:gravity="center_vertical">
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/button_foto"
android:src="#drawable/camera" android:background="#FFF"
android:paddingRight="15dip" android:visibility="invisible"></ImageButton>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/button_comentario"
android:src="#drawable/comment_add" android:background="#FFF"
android:paddingRight="15dip"></ImageButton>
</LinearLayout>
</RelativeLayout>
and the xml block for the ListView would be:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:padding="5dip" android:background="#layout/list_box">
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:cacheColorHint="#00000000"
android:headerDividersEnabled="false" android:footerDividersEnabled="false
</ListView>
</LinearLayout>
and what i use to hide the row when the boolean i told you about is set FALSE is:
wrapper.getDispositivo().setVisibility(View.GONE);
wrapper.getFoto().setVisibility(View.GONE);
wrapper.getComentario().setVisibility(View.GONE);
PS: wrapper is the instance of the class where i have all the elements of the row, i.e. the checkbox (getDispositivo()), and a couple of image buttons (getFoto(), getComentario())
Thanks in advance...
How about using custom dividers in your relative layout and setDivider(null); so once you hide the layout the dividers are hidden as well. I wanted to actually add this as a comment. But it comes only after 50 reps so had to put it as a answer.
I have 3 lists stacked on top of each other in a single Activity, and I would like to set empty views for each of them. I have tried setting the empty view to both dynamically created views and views defined in the layout, but either way all it seems to do when the list is empty is collapse it to 0 height, regardless of the fact that I have set the minimum height for each list to 60px.
This is the xml definition for the empty view
<TextView android:id="#+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="60px"
android:text="#string/empty_text"
/>
mListView.setEmptyView(findViewById(R.id.empty_view));
and this is how I programmatically created the text view.
mEmptyView = new TextView(this);
mEmptyView.setLayoutParams(new ListView.LayoutParams(60, 60));
mEmptyView.setText(R.string.empty_text);
mListView.setEmptyView(mEmptyView);
My bad, this was pretty silly on my part :P I was under the rather ludicrous impression that if I put a TextView somewhere randomly in the xml layout and then set it as the empty view for a list, it would magically disappear and then reappear in the list when the list was empty. I guess I thought that the list would add it as a row in the list when it's adapter was empty. In the unlikely even that someone else decides to try something similarly ludicrous and wonders what's going on, I will post the full version of my original layout as well as the solution.
The code I used to set the empty view was the standard:
mListView.setEmptyView(findViewById(R.id.empty));
however my original layout looked like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200sp"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/drafts"
/>
<ListView android:id="#+id/drafts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60sp"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="#string/finished"
/>
<ListView android:id="#+id/finished"
android:layout_width="fill_parent"
android:layout_height="60px"
android:minHeight="60px"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/uploaded"
/>
<ListView android:id="#+id/uploaded"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60sp"
/>
<TextView
android:id="#+id/empty"
android:text="DUDE WHERE'S MY CAR?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
While my listviews have a minHeight of 60sp, when the list was empty the height seemingly went to 0. As well, I noticed that the TextView was visible below all three of my lists so it did not seem to be "disappearing" and reappearing as a row in my list. How disappointing! In a gross oversight on my part, for testing I set the view as the emptyView for my second list only, ensured that my first and second lists were empty, and populated the third list. My first list had its min-height, my second shrank to 0, my third list was correctly populated, and my empty view appeared below it. I did not try populating the second list, and thus never realized that the empty view would subsequently disappear.
Finally after spending an hour more playing around with dynamically generated empty views and whatnot, I left it for a day and came back tonight and tested populating the second list. Voila, the empty view disappeared and the lightbulb went on. I quickly tested moving the empty view below the second list and above the third list, and of course, it worked perfectly!
Here is the new working layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200sp"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/drafts"
/>
<ListView android:id="#+id/drafts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="#string/finished"
/>
<ListView android:id="#+id/finished"
android:layout_width="fill_parent"
android:layout_height="60px"
android:minHeight="60px"
/>
<TextView
android:id="#+id/empty"
android:text="DUDE WHERE'S MY CAR?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/uploaded"
/>
<ListView android:id="#+id/uploaded"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Long answer, probably just talking to myself, but whatever - asked the question, figured it out, and I never seem to be able to answer anybody else's questions on here so I might as well answer my own :)