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.
Related
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
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"
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);
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!
I have a selection for some items using a spinner widget. At the moment the spinner will load a simple Textview and show the name of the item.
It is possible to define an own view to show inside the spinner row? I would suspect it being similar to a custom List row.
I simply want to show an individual icon left from the spinner text for each item in the list.
Yes, you can do that - as you may know, Spinner is a type of AdapterView, which means that you can adapt your own data to show in such View.
The approach is pretty similar to the one with ListView.
Excerpt from ApiDemos:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
You can define your own layout and pass it to the SpinnerAdapter subclass that you use.
Yes, it is very easy.
The main thing is that, define TextView as the root element in the layout file.
Do as below:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
simple_spinner_dropdown_item.xml file :
<?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="50px"
android:textColor="#000000"
android:textSize="20px"
android:gravity="center_vertical" >
</TextView>