#android Resource IDs - android

In Android layout files, I see the following attribute in a ListView widget:
android:id="#android:id/list"
How can I access that ID inside the Java program with something like the following (which doesn't work)
ListView myLV = (ListView) findViewById(R.id.list);

use ListView myLV = (ListView) findViewById(android.R.id.list); for getting reference to ListView if you have added ListView as android:id="#android:id/list" in xml

Your problem is that you have set #android:id/list id in layout, but trying to find R.id.list where R is resources class from your project
So you got two ways to resolve this problem:
Change id in layout to "#+id/list
Or change id in code to android.R.id.list

Try to change first line to something like this:
android:id="#+id/list"

Related

What are the expected fields of setAdapter method on a ListView?

So I'm trying to build a Navigation drawer. When I try to run setAdapter on my ListView object, I'm not sure what I'm supposed to pass in the second field.
drawerList.setAdapter(new ArrayAdapter<>(this,?,drawerItems));
I referred the official docs but their code is
mDrawerList.setAdapter(new ArrayAdapter<>(this,
R.layout.drawer_list_item, mPlanetTitles));
And they haven't explained the drawer_list_item layout or what it contains.
It's an R.layout resource integer built from a drawer_list_item.xml file that describes how to load the view.
And the documentation does explain it.
The resource ID for a layout file containing a TextView to use when instantiating views.
By default, the objects in the adapter will be loaded into the ListView from their toString method into a Textview with #android:id/text1
The simplest layout is android.R.layout.simple_list_item_1

Is R.layout.listview is same as R.id.listview

I am new to android programming. I have created a ListView and its android:id="#+id/listView1"
ListView list= (ListView) findViewById(R.id.listView1);
ListView list= (ListView) findViewById(R.layout.listView1);
Will it refer the same ListView? Are there any difference between these two snippets?
No Both are different.
R.id.listView1 :-
Represents the id of View which is declared in layout (your XML file) as android:id="#+id/listView1"
and
R.layout.listView1 :-
Represents the layout file (xml file) which into res -> layout dir
You can do
ListView list= (ListView) findViewById(R.id.listView1);
because ListView is of View family.
But you can't do
ListView list= (ListView) findViewById(R.layout.listView1);
android:id="#+id/listView1"
this may be id of the listview created by you in the xml file.
ListView list= (ListView) findViewById(R.id.listView1);
and ou are representing the listview in your java file by calling the id.
ListView list= (ListView) findViewById(R.layout.listView1);
this is the way of representing a layout in your java file by calling the id of the layout
Both are representing different means.
android:id="#+id/listView1"
represent an widget inside an layout (XML file) that have Attribute as follows :-
android:id="#+id/listView1"
where as R.layout.listView1 shows you have a XML file in your layout folder named as listView1.
so both are different.

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

Error: You must supply a resource ID for a TextView

I have list that retrive a form my sql with (course id- course name).
I use a hash her for the list, I create an adapter which give me the list and I set it to spinner.
However there is a problem in log cat that said:
as Very well Explain by Luksprog :
The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be an xml layout where the first element must be a TextView, something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
//other attributes
/>
If you want your list row layout to be something a little different then a simple TextView element use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout, this.file)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings
If you check http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, T[]), you will find that the array adapter is expecting that the second parameter is a resource id of a text view not a layout..
initialize your adapter using:
new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, coursesList);

Changing ListView text color

I have ListView that shows data:
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
The headlines is an array:
List headlines;
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
How to change the text color, style and size? There isn't any android:textColor on the ListView.
Find the file simple_list_item_1.xml from your sdk installation, copy it to your project's layout folder, rename it and make whatever color/text changes you want in there. Then replace the layout call in your program with your new layout name (don't forget that since you will be using your own list row layout you need to remove the android. from in front of the layout call).
It should look something like this:
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.your_row_layout, headlines);
Hope this helps!

Categories

Resources