Assign ListAdapter to ListView in layout - android

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().

Related

ListView and ArrayAdapter

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

List footer view doesn't appear if setListAdapter is called

I am having a somewhat weird scenario building the UI for an Android application. Although I got to solve the error, I don't know the cause and I would like to understand why this is happening in order learn more about Android. I reproduced the scenario in a simple application:
Creating a new Hello World Android application adding this resources file under res/layout/footer_view.xml:
<?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="wrap_content"
android:gravity="center_horizontal"
android:text="Foo Bar" />
And using this Activity:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(android.R.layout.list_content);
ListAdapter adapter = new ArrayAdapter(this, android.R.layout.activity_list_item);
setListAdapter(adapter);
View footer = LayoutInflater.from(this).inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footer);
}
}
We get the following screen:
To my surprise I saw that the footer view, which is a TextView saying "Foo Bar", was missing. After some playing around I found out the solution was to move the "setListAdapter(adapter);" line to the end of the onCreate() method, or said differently, after adding the footer to the view.
Exploring a little further I found out the problem is the width, which is 0 if I have the adapter added before. Another solution was to indicate an explicit width other than match_parent.
Any clue why this happens? For me it'd be more intuitive that it didn't work if the adapter hadn't been added because the list may not be ready, but why would it not work if everything is prepared?
The reason is that internally, if there are headers or footers, the adapter is wrapped inside another adapter.
So if you call setAdapter before adding the header and footer views, then the wrapping adapter is not aware of them.
If you leave setAdapter to the end, then the adapter that wraps your adapter knows about the headers/footers since those have already been added to the list.
See the documentation:
Add a fixed view to appear at the bottom of the list. If addFooterView
is called more than once, the views will appear in the order they were
added. Views added using this call can take focus if they want.
Note: When first introduced, this method could only be called before
setting the adapter with setAdapter(ListAdapter). Starting with
KITKAT, this method may be called at any time. If the ListView's
adapter does not extend HeaderViewListAdapter, it will be wrapped with
a supporting instance of WrapperListAdapter.

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

still didnt get #android:id/list vs #+id/listView1

when extending listactivity it is must to have a #android:id/list
question 1> what if i want to implement 2 listviews then ids will be same #android:id/list.
question 2> in what scenario i should use #+id/listView1.
Thanks in advance.
Well, the ID is independant(unless you don't name them uniquely) so you'd use the "#+id/listView1" solution every time as far as I know. I did this on my own Android app and it worked just fine.
ListActivity is basically a 'convenience' class which simply extends Activity and has a single ListView plus some convenience methods for handling it - there's nothing particularly special about it.
If you want an Activity which has more than one ListView then it would be better to simply create your own from scratch.
If you create a Xml with let's say a relativelayout that contains a imageview on the top and a listview in the middle and a button in the bottom. Then you need to set an uniqe ID for your listview to gain access to it.
ex:
<ListView
android:"#+id/unique_listview"
android:layout_width="fill_parent"
android:layout_height="400px"
/>
Then you can customize and populate the listView with this:
ListView myList = (ListView) findViewById(R.id.unique_listview);
myList.setAdapter(new myListAdapter(this));
private class myListAdapter extends BaseAdapter {
....
}

Change ExpandableList view programmatically

I have an ExpandaleList, and BaseExpandableListAdapter which I implements.
Now sometimes I need to show other xml content, So I am trying to do:
ExpandableList.this.setContentView(R.layout.errorscreen);
but then I get this exception:
ERROR/AndroidRuntime(23948): java.lang.RuntimeException, Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'.
Now, I have been told to do this in order to fix it:
add ExpandableListView to your xml layout and set it's id to list. Like so:
<ExpandableListView android:id="#id/android:list" android:layout_width="fill_parent"android:layout_height="fill_parent"/>
How to do it programmatically?
You are setting this Activity's contentView to a Layout which is defined in an xml file called errorscreen.xml:
ExpandableList.this.setContentView(R.layout.errorscreen);
The activity you're doing this from however extends ExpandableListActivity (I guess). Such an Activity needs an ExpandableListView in it's xml to display it's data. Now simply open your errorscreen.xml and add this element.
Cheers

Categories

Resources