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);
Related
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
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'm having some trouble figuring something out that should be quite easy and straight forward. I have an activity that has a few Views in it including a ListView that currently is not populated.
How do you programmatically get a reference to that ListView and then set the items, each item being a TextView with the content then being XML String-Array items?
Getting reference I suppose is like this:
ListView incomeList = (ListView) findViewById(R.id.incomeList);
Also I have a this code in my strings.xml(Want to populate list with this array):
<string-array name="testArray">
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
<item>Item4</item>
<item>Item5</item>
</string-array>
And then lastly I have this other layout xml file that I should have according to all the tuts out there. list_item.xml
<?xml version="1.0" ebcoding="utf-8">
<TextView xmlns:android="blablabla..."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
</TextView>
All of the tutorials I followed on the web though is helpful if a ListView is the only View in your activity. Whereas in my case I have quite a few Views including buttons, textviews etc etc.(If this might be helpful to know). Also do I extend my class with ListActivity or since I have other views in there, just extend normally with Activity?
You don't need to extend ListActivity. Just extend a regular activity.
Steps:
A) get your ListView. You're doing it right in your question.
B) create an ArrayAdapter. You must pass a Context (if you do it on the Activity, pass that Activity), an int representing a Layout which ONLY contains a TextView (Like the one in your question), and a String[] containing all the items you need.
Should look like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, getResources().getStringArray(R.array.testArray);
C) lv.setAdapter(adapter);
Pretty straightforward.
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!