My activity has a spinner and a custom ListView and they are both sourced by custom ArrayAdapters (ArrayList of custom objects). The ListView rows are custom views (just 2 TextViews).
When I include the line, spinner.setAdapter(spinnerArrayAdapter); the ListView is NOT filled with data on the screen and the spinner contains the values I want.
When I remove the line, spinner.setAdapter(spinnerArrayAdapter); the ListView is filled with the data I want and the spinner does NOT contain any values.
When I debug, I see the data in the ListView's custom ArrayAdapter variable and the spinner's custom ArrayAdapter.
I have checked my getView() implementation for the ListView's custom ArrayAdapter class.
Suggestions?
Thanks in advance!
Can you paste some code to help identify the problem. I tested with two listviews and a spinner pointing to the same adapter. It worked fine for me.
Related
Is it possible to have some listview rows have a checkbox, or a switch and others to nothing just a title and remain clickable.
I am trying to a do a listview that has a few different settings.
First - Checkbox
second - Switch
Third - Just list.
I know I could do them as separate buttons I would like to have them filled from the same array if possible?
YES Definately Its possible...
1. Create a custom layout for ListView or RecyclerView row item with CheckBox, Switch, TextView or other views as per your requirement.
2. Create a custom adapter extending from ArrayAdapter, BaseAdapter or RecyclerView.Adapter to populate list data to your custom layout.
3. In getView() or onBindViewHolder() method, set data to your views and add required OnClick or OnCheckedChange listener to your Views.
Here are some tutorial link:
Android Custom ListView with CheckBox
Android RecyclerView with CheckBox
ListView with CheckBox using custom Adapter class
Android ListView with Checkboxes
Android Custom ListView with Image and Text
Hope this will help~
I have two listviews with custom adapters. The first listview contains names of lists. When I click on the name of a list, the second listview have to be populated with the contents of the clicked list. I need to read the contents of the list from a local SQLite database that's located in assets, but that's not important. Assume that I have array of the contents. I know how to do this if I have to populate them on button click, but I don't know to do it this way. Both listviews are in the same activity.
by using onItemClickListener of listview u can do this.
on onitemclickListener of first listview u set adapter for the second listview
u can do this using one customadapetr and u can write separate customadapter for both
listview.
if u post some sample code i will try to give more specific answer
Without using listactivity, in a simple activity how can I have checkbox at the left followed by the text in the right for every row of a listview. If I tap on any row , the check box should be selected.
Just get reference from xml, using:
listView=(ListView)findViewById(R.id.listView);
Set adapter on your activity
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arr);
listView.setAdapter(adapter);
setChoice Mode to single choice mode or multiple choice mode
listVIew.setChoiceMode(ListView.Choice_Mode_Mutlitple);
now you can select and deselect checkboxes in listview.
Check out this tutorial - it will give you an idea how to implement custom listview item layouts.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
To make the checkbox change state when any part of the whole item is clicked, add an OnClickListener to your item's layout root element (convertView) and switch the checkbox state there.
PS Don't mind the ListActivity stuff there. It will work either way. What matters is the custom Adapter and it's getView() method that inflates custom layouts of your items.
UPD Here's a more adequate way to accomplish what you want: your items should implement Checkable and then you can use ListViews singleChoice or multipleChoice mode with it. Here's an answered question about that, although, the answer seems to miss the actual part about the checkbox. But I bet you can figure it out, what matters is the idea:
Android ListView with RadioButton/CheckBox in singleChoice mode and a custom row layout
I have set a dynamic value in a spinner . I am using following code for the same.
spinner_generalbooks.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, new String[]{"Author","ISBN","Keyword","Title"}));
It is working fine, but I have a problem with the view of the spinner . If we select simple_spinner_item, it is ok in normal state, but when we want to change it, the rows are very narrow and not CheckedTextView whereas in normal spinner options comes with CheckedTextView. If we select simple_spinner_dropdown_item, the options come with CheckedTextView, but in normal state, it looks different as in the pic
(First one is default spinner and second one is using simple_spinner_dropdown_item).
I want to show the spinner just like as default spinner. How to make it?
Android will take the layout specified in the adapter and use it for the control and the items unless you specify the view-resource separately. The way through this is to set the layout to simple_item in the ArrayAdapter constructor and then set the layout dropdown_item separately in a call to setDropDownViewResource().
ArrayAdapter newAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, new String[]{"Author","ISBN","Keyword","Title"});
newAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_generalbooks.setAdapter(newAdapter);
If you whant to customize the visual of your Spinner, it'll be more simple to create your own component. A spinner is just a Layout that contains text, image and that display a list in a popup. Create a custom layout for your item and use a new BaseAdapter object to bind your datas.
http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/
hope i could help
I'm trying to add a CheckBox to every item in my ListView. I can't put a CheckBox widget in the ListView, so how do I do it? I think the Android settings has something like this.
Off the top of my head, you should create an xml layout file representing an item in the ListView, in which you would declare a CheckBox. You can then set the adapter to use your layout for every item as follows:
ArrayAdapter<T> adapter = new ArrayAdapter<T>(this, R.layout.list_item, listReference);
setListAdapter(adapter);
setListAdapter is a method in ListActivity. Your list_item.xml should have the CheckBox defined in it.
You need to extend a view adapter class and provide your own logic for creating the views for each row. Here's an excerpt from an android development book at commonsware.com that might help.