I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.
Related
I'm having an issue with my spinner. When I debug, I can see that there is data : my adapter contains objects.
Debug spinner adapter screenshot
However, the spinner appears like it has nothing in it :
My empty spinner
Here is the (supposed) interesting code :
`
ArrayList<ActivityToSteps> activityConversionList;
ArrayList<String> activityList;
ArrayAdapter<String> categories_adapter;
activityConversionList = new ArrayList<>();
activityList = new ArrayList<>();
categories_adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, activityList);
categories_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
activityConversionList.addAll((ArrayList<ActivityToSteps>)task.getResult());
for(ActivityToSteps ats : activityConversionList){
activityList.add(ats.getActivityName());
}
categories_adapter.notifyDataSetChanged();
`
ActivityToSteps is a class I created, containing a String "ActivityName" attribute and a float "StepsPerMin" attribute.
I'm getting a list of ActivityToSteps from an async task, and I want a spinner containing all their "ActivityName", stored in the ArrayList "activityList" (which is not empty, but can't show screenshot beacause reputation < 10...).
I hope I've been clear enough!
Thanks in advance for your time!!
Please double check that you have passed your list to spinner adapter.
your_categories_spinner.setAdapter(categories_adapter);
just forgot to set the adapter to the spinner..
spinner.setAdapter(categories_adapter);
I have a listview of that's tied to an array adapter. For the life of me I can't figure out how to get a list of the checked boxes in the listview.
CheckViewArrayAdapter adapter;
int[] intarray;
paramListView = (ListView) findViewById(R.id.datalog_paramselectlist);
// get all supported params
intarray = ConMan.Ecu.getSupportedParamArrayVals();
LinkedHashMap<Integer,String> hm = new LinkedHashMap<Integer,String>();
for( x=0;x<intarray.length;x++){
hm.put(intarray[x] , ConMan.Ecu.paramToText(intarray[x]));
}
adapter = new CheckViewArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice , android.R.id.text1, hm);
adapter.setBoolArray(ConMan.Ecu.getSelectedParamFlagArray());
// Assign adapter to ListView
paramListView.setAdapter(adapter);
I have a setOnItemClickListener for paramListView that works, but I just want to get the final set of checked checkboxes when the screen exits. I simply don't know where to look.
I have a setOnItemClickListener for paramListView that works, but I just want to get the final set of checkboxes when the screen exits.
I assume that you are not setting the choice mode for your ListView. If you do use:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
then in your onPause() method, you can ask for an index of the checked rows with ListView#getCheckedItemPosition() and you don't need to change the checked state manually in your OnItemClickListener.
I have a listview displaying list of items and a button at the bottom. On click of button, i'am displaying one alert dailog box to take input from user and adding that input to the listview. 1. How can i make sure the new input/entry should add always at top of listview? (Right now it is adding at bottom always)2. Is it possible to position the list view at the new entry? (i.e if the new entry is added at top, the list should be positioned at the top)Please guide me.
Inside onCreate()...
list = (ListView)findViewById(R.id.planetList);
adapter = new ArrayAdapter<String>(MyListViewActivity.this,
R.layout.my_list_row, R.id.planetNameTextView);
for (int i = 0; i < planetNamesArray.length; i++) {
adapter.add(planetNamesArray[i]);
}
list.setAdapter(adapter);
After taking input from user ....
adapter.add(newPlanetNameEnteredByUser);
adapter.notifyDataSetChanged();
you can use listview.setStackFromBottom(false) to fill your listview from top.
this link may help you.
http://developer.android.com/reference/android/widget/AbsListView.html#setStackFromBottom%28boolean%29
Yes you can do that, have two List.
List<String> old = new ArrayList<String>(); // contains your list content
List<String> new = new ArrayList<String>();
Now, when you want to add new content to ListView then add it first to the new ArrayList,
new.add("new content");
new.addAll(old); // add all the old contents
new.setSelection(position_you_want);
adapter.notifyDataSetChanged();
Add the object in array list
ArrayList<String> List= new ArrayList<String>();
ListView ListName= (ListView)findViewById(R.id.playerdata);
with
List.add(index, "value");
public void add (int index, E object)
Since: API Level 1
Inserts the specified object into this ArrayList at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this ArrayList, the object is added at the end.
Parameters
index the index at which to insert the object.
object the object to add.
Throws
IndexOutOfBoundsException when location < 0 || > size()
and set list adapter
ListName.setAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1 , List));
I have a spinner set up like this:
ArrayAdapter<String> states = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.stateabbrev));
states.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(states);
As you can see, the source is an array.xml file.
I want to know how to populate it if I know the array value. For instance, I am retrieving information from my database and the user is from "KY" so I have a string "KY" and I want the spinner selection to be on "KY"
at first we should get position of "KY"
int position = states.getPosition("KY");
after that, select in spinner with position
state.setSelection(position);
I'm using a MergedAdapter to group two custom adapters (ArrayAdapter derived) along with a section header for each one into a single ListView. It is working fine, but now I need to display a TextView saying "No Data" for the section that has no items, e.g. ArrayAdapter is empty.
What's the best approach for this? The code that sets the ListView binding is like this:
ArrayList<ItemOne> firstItems = getFirstGroupItems();
ArrayList<ItemTwo> secondItems = getSecondGroupItems();
ItemOneAdapter firstAdapter = new ItemOneAdapter(this, this.firstItems);
ItemTwoAdapter secondAdapter = new ItemTwoAdapter(this, this.secondItems);
MergeAdapter adapter = new MergeAdapter();
adapter.addView(createGroupSeparator(R.string.first_section_header)); //Just creates a TextView
adapter.addAdapter(firstAdapter);
adapter.addView(createGroupSeparator(R.string.second_section_header)); //Just creates a TextView
adapter.addAdapter(secondAdapter);
listView.setAdapter(adapter);
In case of an empty list, you could make the first item read "No Data", but this usually makes the code unmanageable. I would suggest you add an invisible TextView to your screen with the message; this is only displayed when the list is empty.