ListView and ArrayAdapter - android

When we build a ListView in android studio, we need to use an ArrayAdapter.
What is the task of second argument in constructor of ArrayAdapter ?
I cannot understand what is android.R.layout.simple_list_item_1 used for ?

This layout describe how the item list looks like.Maybe you want every item contain a text view and image.You should specify these details in this layout.

android.R.layout.simple_list_item_1 is a layout in which the data from your ArrayAdapter gets populated(added).

This is the id of the layout that you need to use for populating each of the item in the list. If it says android.R.layout that means you are going to use one of the standard android layouts. simple_list_item_1 This is the name of the file which will populate each row of the list. try changing this to simple_list_item_2 to see how the layout in list changes.
You can also use your custom adaptors and custom layouts(which would be in majority of the cases in day to day apps).
For full list of standard layouts available Go here

This argument defines how list items would appear in ListView, There are many layouts you can also try them out or you can make your own custom layout to modify apperance of listitems in Listview by using CustomAdapter.

1.create custom layout name custom_layout.xml & paste bellow code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp" >
</TextView>
Use like R.layout.custom_layout instead of android.R.layout.simple_list_item_1

Related

ListView Create cell programmatically

I need to put a series of checkboxes in the cell of a ListView. The problem is that I do not know ahead of time how many. I am guessing, but please correct me if I am wrong, that I need to create it programmatically. There will only be a few cells so I am not worried about reusability. How do I approach doing that?
No xml just build the cell in the getView method --> is this as simple as creating a View and add to it?
xml but append checkboxes to the xml --> I have no idea how to do this.
Is there some sort of dynamic xml CheckBox list that I can use?
I always prefer to use XML to separate the presentation of the controller (or code that controls the app)..
You just need to create a list view and define it's adapter. The adapter will have an XML (a row) where you can design the checkbox and the other elements that you want...
Check out the documentation where you can see an example of a listview with an adapter.
-- edit:
In your case you need to add the checkboxes programmatically in your adapter. Just define a view in your xml where you can add your checkboxes.
-- edit2:
Here is an example XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/checkboxContainer" >
</RelativeLayout>
</RelativeLayout>
In your adapter you
//get the container
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.NAME_OF_YOUR_XML, parent, false);
RelativeLayout your_container = (RelativeLayout) rowView.findViewById(R.id.checkboxContainer);
//declare your checkbox
CheckBox cb = new CheckBox(context);
cb.setText("your text");
// add the checkbox to your container
your_container.addView(cb);
This is just an example. I didn't test the code. Maybe you should use a linear layout instead of a relative layout to place your checkboxes easily..
easiest and most flexible is to use the ArrayAdapter and make an XML layout that looks like the single cell you'd want. Put your data in the array, create the Adapter and assign it to the ListView, and boom, your list has the right number of rows (cells). You can also customize the binding easily so that each cell has some information based on the corresponding Array entry.

Dynamically adding subitems to ListView in Android

I am trying to dynamically add information to a ListView. The information I am adding consists of a "Device Name" (the main item) and "MAC Address" (the sub item). An example from online is below. Note: I want to replace Item 1 with a device 1's name, sub item 1 with device 1's MAC address, and so on. This MUST be done dynamically because the list is being populated as devices are scanned for.
.
Before this is marked as a repeat, I have looked at the following questions and they have not helped me: Adding ListView Sub Item Text in Android, How to add subitems in a ListView, Adding Items and Subitems to a ListView
The conclusion I have come to through reading these questions is that I need to implement a custom ArrayAdapter and override the getView() method. I have created a custom layout with two text views in it:
cyan_list.xml
<?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" >
<TextView
android:id="#+id/main_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/cyan"/>
<TextView
android:id="#+id/sub_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/dark_cyan"/>
</LinearLayout>
I then try to create a custom ArrayAdapter in my Activity class, but I am lost as to what to put in my public View getView(final int position, View convertView, ViewGroup parent) method. Additionally, is creating a custom ArrayAdapter necessary if all I am trying to do is add a textview sub item?
The answer to your question is: NO, you don't need to create a custom ArrayAdapter if you just want to add items. I recommend, however, creating it if your layout is customized, as you'll gain so much control over the items you're displaying. You didn't add your code where you create your ArrayAdapter, but in your case I'd use this constructor. The important part is the third parameter: In your activity, you should store an ArrayList with the initial items you're adding to your ArrayAdapter, then, if you want to add a new item, you simply add it to the ArrayAdapter and call notifyDataSetChanged() on your adapter. Simply doing that, your item will be added to the layout and displayed. If you need to override the GetView method for your own ArrayAdapter, I recommend this link, it helped me understanding the whole thing.
are you searching some listview example in google like those tutorials :
http://www.vogella.com/tutorials/AndroidListView/article.html
http://www.mkyong.com/android/android-listview-example/
I think they explain step by step how to create a list adapter
You need to add getter method into your Adapter
YourAdapter ...{
List<Device> items = new ArrayList<Device>;
public List<Device> getItems(){
return items;
}
}
then change item that you need
...{
//for 1s item
Device item = getItems().get(0);
item.setTitle(macAdress)
}
and call notifyDataSetChanged for your adapter
...
yourListView.getAdapter().notifyDataSetChanged();
}
Thats it. Now you are able to change your list data.
And for your question, i think yes. Is better to create your own adapter in order to have simple possibility to exentd it later. And in your case (if you dont want to change your adapter after each title change) you deffinetly need custom one. Cheers

Assign ListAdapter to ListView in layout

My main activity has a layout containing a ListView and several other items. I'd like to assign a ListAdapter to it. I followed the tutorials shown here: http://www.vogella.com/articles/AndroidListView/article.html
How can I let the adapter know that my activity's layout has a ListView in it, and assign it to it? When I try to run the app as-is it crashes with a runtime exception stating that my layout needs to have the default Android listview in it.
Thanks!
If your activity is inherited from ListActivity, you don't have to manually let the adapter know the ListView.
But in order to do this, ListView in your activity should look like this
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
because documentation says
your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)
When I try to run the app as-is it crashes with a runtime exception stating that my layout needs to have the default Android listview in it.
It sounds like you're using a ListActivity and calling setContentView(). In the layout that you pass to setContentView() you must have a ListView with the following attribute:
<ListView
android:id="#android:id/list"
... />
Then bind your Adapter to the ListView with setListAdapter().

How to wrap lengthy text in a spinner? [duplicate]

This question already has answers here:
Spinner does not wrap text -- is this an Android bug?
(15 answers)
Closed 3 years ago.
I have two spinner and EditText controls within a table layout view on a separate row. The spinners are populated with data. My problem is the data (texts) that are populated into the spinners are too lengthy to fit the screen size. Therefore, the spinners are forced to stretch unnecessarily stretching other controls on another row.
It's a must for me to show the texts in the spinner. Hence using ellipses is not an option. If it's possible how can I wrap the lengthy text on the spinners?
Step 1. TextView with wrapped text
The first thing to do is to to force simple TextView to wrap text. Its easy:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="very long text that will be wrapped to next line" />
Note the singleLine attribute here.
Step 2. Custom layout
Now we should somehow set singleLine attribute to false in TextView used by Spinner to show the item in the list.
In your code you probably have place where you create adapter to use it with Spinner:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
android.R.layout.simple_spinner_dropdown_item);
The idea is to copy the android.R.layout.simple_spinner_dropdown_item layout to your project. Then modify it by setting singleLine attribute to false in CheckedTextView:
For this, add file to res/layout folder named multiline_spinner_dropdown_item.xml with next code:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="false"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
Note that this file is identical to android.R.layout.simple_spinner_dropdown_item layout, except it has singleLine set to false now.
Step 3. Creating Adapter with custom layout
Modify your adapter creating code to:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
R.layout.multiline_spinner_dropdown_item);
Here is screenshot from modified SpinnerActivity example from Android SDK:
Define a custom layout and use it with the spinner and adapter.

How to change appearance of ListView?

I have three main things I want to change with the appearance of a ListView.
1) get rid of horizontal dividers that print between items
2) change font size of test displayed in listview
3) change the padding around each item displayed
It looks like after reading for a while that 2) and 3) should be controlled by changing these properties for the TextView used by the ListView, but I don't yet understand how to do that. Can someone answer these with a little more detail?
1) Set divider height to 0 --- setDividerHeight(0) and set divider color to transparent --- setDivider(new ColorDrawable(0x00FFFFFF))
2) If you're using a list of text views then you can continue using a simple adapter like ArrayAdapter but you will need to create a custom text view. You can add something like this to res/layout
test_text.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
3) Add padding to your textview above
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
Use your new layout with your ArrayAdapter
eg.
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.test_text, R.id.text, myData);
All of those can be controlled via styles. The docs on styles explain everything you need to do.
In short, you create a style resource - derived from whatever you want to use as your base - where you override all the relevant items, like the divider, the padding the font size.
Then, you either apply that style to your ListView in the XML file, or you create a theme to it and call setTheme() BEFORE you call setContentView().
EDIT: I should mention the obvious answer that kcoppock pointed out - if this is specific to just one element in your layout, just modify that particular element in your layout file.

Categories

Resources