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/
Related
I am building an android application which will send and recieve messages from a server (using JSON and HTTP sender).
Now i thought that it could be cool if the messages were displayed just like when you send or recieve an SMS.
for example:
Picture
I have been trying to google around for some hours now, but all i find is how to send an sms from your android application.
Does anyone know if this is possible?
To create this layout you will need an activity with two fragments. The first fragment will just contain the view of your editor. The second fragment will be a ListFragment that displays the message results. The list fragment will inflate a custom view, that consists of a background image (which is the little speech bubble) and 2 TextView widgets that contain the date and message respectivley, and an ImageView with an onClick listener (or a ImageButton) for the star.
Here is a sample of the overall activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.your.package.MessageListFragment"
android:id="#+id/message_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<fragment
android:name="com.your.package.EditorFragment"
android:id="#+id/editor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
And here is a sample of the fragment that is the message entry interface
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#252525"
>
<Button
android:id="#+id/attach_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/picture_attach_button"
android:onClick="onAttachPicture"
/>
<Button
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/attach_photo"
android:padding="8dp"
android:text="Send"
android:onClick="onSendClick"
/>
<EditText
android:id="#+id/message"
android:layout_alignParentLeft="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/send_buton"
android:layout_centerVertical="true"
android:hint="Enter messaage.."
/>
<ImageView
android:id="#+id/attached_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_above="#id/message"
/>
And for your ListView fragment make a slight modification to the default list view to add some margins (there are other ways to do this)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#252525"
>
<ListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:marginLeft="8dp"
android:marginRight="8dp"
/>
<TextView android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="this is an empty list"
/>
And the adapter you attach to your ListView will have to return a custom UI element that looks like this see here how to implement a custom ListView:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/speech_bubble"
>
<ImageButton
android:id="#+id/favorite_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onFavoriteClick"
android:src="#drawable/star"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<TextView android:id="#+id/response_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/favorite_button"
android:text="this is an empty list"
/>
The speech_bubble drawable should be a 9patch image that does not scale the corners or the indent of the bubble.
You do all of the applications back end work in the onClickListeners (i.e. onSendClick, onAttachPicture, onFavoriteClick). You can populate your custom ListView in any number of ways, but one way is to use a BroadCastReceiver that listens for incoming SMS messages, or define your own receiver in your backend code.
Listview + 2 different layouts for list items (received and sent). Also check this to help you implement a listview with two item types.
I have ListView with layout for row like which I inflate at adapter ( extend BaseAdapter ) at getView method
<?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="fill_parent"
>
<CheckBox
android:id="#+id/chk"
android:button="#drawable/q_list_check_box"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/txtChoice"
android:textColor="#color/white"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I put at ListView tag android:choiceMode="singleChoice". How to make list single choice, that only one row can be checked at time ?
When I had to implement selection with my custom row., I used an arraylist of boolean to keep the state of the row. The size of boolean arraylist is same as rows of the listview. You can select/deselect the row via changing the value of corresponding boolean in OnListItemClick(). Anyways its just an idea. Here is a link that can help to understand this:
Custom List row with checkbox
I have three different activities sharing exactly the same layout (a ListView and a TextView). The ListView uses this layout for its SimpleCursorAdapter:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="2dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/locationName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Länsnamn"
android:textAppearance="?android:attr/textAppearanceMedium">
</TextView>
<LinearLayout
android:id="#+id/num_stores_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/numStores_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10dp"
android:text="Antal butiker: ">
</TextView>
<TextView
android:id="#+id/numStores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="0">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/numItems_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10dp"
android:text="Antal artiklar: ">
</TextView>
<TextView
android:id="#+id/numItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="0">
</TextView>
</LinearLayout>
</LinearLayout>
The program goes from activity #1, user picks an item in the list, activity #2 starts, user picks list, activity #3 starts.
Now, all the list items turn up fine just like they should, but for activity #3 I'd like to remove the entire row with id num_stores_row, and I seem to have a hard time doing this. I cannot do this in the setViewBinder since the row is not targeted by the adapter, and I cannot do it outside since the ListView is not activated until the data arrives. I've tried findViewById(R.id.num_stores_row).setVisibility(View.GONE); here and there, but the simulator crashes every time.
How do I remove it?
To add to what Mark said, override SimpleCursorAdapter's getView and put your findViewById(R.id.num_stores_row).setVisibility(View.GONE); in there once you inflate the row's view.
Do not include that row in the Cursor that you wrap in the SimpleCursorAdapter. Or, create your own subclass of SimpleCursorAdapter that overrides a bunch of methods (e.g., getCount()) to filter out that row.
I have a listView that I want to dynamically add data to.
here's the XML I want to add the data to.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:padding="12dp"
android:textSize="16sp" >
</TextView>
<TextView android:id="#+id/alarm_name_text"
android:layout_height="wrap_content"
android:text=""
android:layout_below="#+id/rowTextView"
android:layout_width="fill_parent">
</TextView>
<CheckBox android:id="#+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true" android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
I want to add data to the 2nd TextView with the id alarm_name_text after a user has entered some data in an editText dialogView. I've been told to make a function that adds the data the my ArrayList/adapter but I'm not sure when to call it or how it's being used.. I needs help plz :(. Here's the function.
public void addItems(View v)
{
rowSavedText.setText(getString());
planetList.add(new Planet("This one"));
listAdapter.notifyDataSetChanged();
}
You'll need to work on the part of the code that renders the individual rows. Take a look at the tutorial at http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx: specifically how it uses getView.
Just add the element to your ArrayList and call notifyDataSetChanged() to your array adapter.
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.