Spinner first item default set empty value? - android

Here in this code i have added pincode numbers from 600000 to 600113 in the spinner but i want the first position to be just empty. When the user clicks only then the item should be shown. Please see my code -
final String[] myarray=new String[114];
for(int i=0;i<114;i++)
{
myarray[i]=String.valueOf(a);
a++;
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,myarray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
addnum_spinner.setAdapter(adapter);
lv.setOnItemClickListener(itemClickedListener);
//Spinner click
addnum_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
String value = myarray[position];
//
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

add this property to your spinner in xml and try if that works
android:prompt=""

I don't know if this is the right way but what about just adding the empty string before the for loop as the first item and then you just have to handle the user clicking on the empty item in your Listener. Something like
final String[] myarray=new String[115];
myarray[0] = ""
for(int i=1;i<115;i++)
{
myarray[i]=String.valueOf(a);
a++;
}

I know this question is old, but I'm adding a solution. I found this out on accident, as I DIDN'T want a blank default value, but there was one.
If using an ArrayAdapter, I found that attaching the adapter to an empty ArrayList, and THEN filling it left a blank initial value.
Filling the ArrayList first, then attaching the ArrayAdapter removed the blank default value.
Obviously, if not using an ArrayAdapter, using the prompt, or setting the first Spinner item as <item></item> with nothing in between in the XML should work.

Related

Check all of adapter elements in ListView

I have CustomAdapter which I am using for populating ListView with some data.
Each element in ListView has two variables. For each listview (in onItemClick method) I must check this variables and If they are the same - do some code and If they are different - do another code, for example Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
So I have tried this:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i=0; i<=items.size(); i++) {
SomeItem item = items.get(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
}
});
But all of my listview elements have values of the first element in those two variables.
So how can I do something like item.next(); for validating all of items in listview?
UPD:
Sorry, I will provide more information about what I am doing after checking variables of listview items for understanding my issue.
I have one more adapter:
SomeAnotherAdapter adapterPr = new SomeAnotherAdapter(this, R.layout.list_tem_another, itemsAnother);
and one more listview:
listViewAnother.setAdapter(adapterPr);
First of all I understood, that first variable should be from first listview and the second variable from another listview.
In this listViewAnother I have many items, which has some "id". For example 1st, 5th and 20th elements have id 90 and other elements have id 100.
We can say, that items from the first listview also have "id".
So I must check if(first variable = second variable) and then show in listViewAnother only items that have id which equals ID from clicked item in listView.
I tried: adapterPr.remove(item2); but then I understood, that I need all of items because I can go back to listView and press another item which will need those removed elements.
Now, hope I provided full information and you will be able to help me improve my code.
Do you need to perform the check on every element of the adapter when you click on one element of the adapter? If not, you don't need a loop. If you do, your loop should be iterating over the original list, and does not need adapter position at all.
In general when using adapters and lists, you should use the adapter's position and the adapter's data set to perform any tasks. It's not good practice to use the adapter position to get an item from the original list.
Simply set one onItemClickListener which gets the corresponding item from the adapter, and do what you need to from there:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeItem item = adapter.getItem(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
});

How to disable and enable Spinner items on user click

I am developing an android app, which asks security questions after Sign Up for forgot password purpose, which has 10 questions in total. User can select any 3.
I have 3 Spinner for 3 question. Once user select the question from first spinner, second and third spinner should not have them in their list. Please help me to disable or remove that from the list.
screen shot of the activity
First, set a boolean check if it is the first time a spinner is selected. Store the selected item, so that you can add them on question change later.
Boolean ifFirstCheck = true;
String storeItem = "";
Then, Use below code:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(!ifFirstCheck){
listSp2.add(storeItem);
sp2adapter.notifyDataSetChanged();
listSp3.add(storeItem);
sp3adapter.notifyDataSetChanged();
}
String selectedItem = spinner1.getSelectedItem().toString();
listSp2.remove(selectedItem) // Get selected value from spinner1 and remove thar item from spinner2
sp2adapter.notifyDataSetChanged(); // Notify adapter of spinner2 to that dataset has been changed
listSp3.remove(selectedItem)
sp3adapter.notifyDataSetChanged();
storeItem = selectedItem;
ifFirstCheck = false;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Do as above for rest of the spinners.Hope this helps.
I assume you are using an Adapter for the Spinners. If you use an ArrayAdapter and each adapter holds the same list of items, you can just remove the selected items from the list and notify the adapters to update.

Android:Checked Listview Item Goes at Last

My ListView looks like this:
[CheckBox] [TextView]
My question is, how can I change the item position when CheckBox is checked? I mean to say, if the user checked any item from ListView, then the checked item goes to the end, so its position changes from current to last.
Thanks in Advance.
Use Custom adapter for listView. here is an example. Now from your ListActivity class
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, Object> map = (HashMap<String, Object>)
lv.getItemAtPosition(position);
// get the item Position from here and make the nacessary changes
// and update the listview again.
}
Without posting any code, this will have to be a brief overview of the pseudo steps you need to take
You simply need to update the ordering of your data set being used by your adapter (usually an arraylist or array of objects), and then called
notifyDataSetChanged()
on your adapter.
So in your case, you want to take the element at the position that was clicked, and put it at the end of your arrayList of objects.
If you post some code, you may get more detailed answers however
Here is the step you can follow. I have not tested it but you can try this.
// your value array you are binding with listview
private final String[] values;
// put default value to "n". size will be the same as for values array.
private final String[] valuesChecked;
onClick of Checkbox Listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get checkbox is checked or not
if it is checked then{
valuesChecked[position]="y";
}else{
valuesChecked[position]="n";
}
// short your values array by first "n" and after "y".
// call your adapter.notifyDataSetChanged();
}
Good Luck :)

Why is onNothingSelected method needed in spinner listener?

native English speaker, so I'd say sorry about my bad English skills to you guys.
I've been studing Android since 5 weeks ago. I tried to implement a spinner and my mentor asked why the onNothingSelected method is needed. I had nothing to say.
So, why do I need that method?? Can you reply it?
Following code is my spinner. It does correctly what I intended.
public class SpinnerViewPractice extends Activity {
private Spinner spinner;
private String spinner_value = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.spinnerviewpractice);
spinner = (Spinner)findViewById(R.id.spinner1);
String[] str = {"","good", "dislike", "like", "hate", "moderate"};
spinner.setPrompt("Set Text");
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, str);
spinner.setAdapter(list);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView tv = (TextView)arg1;
spinner_value = tv.getText().toString();
if(spinner_value.length() == 0)
{
spinner_value = "Nothing";
}
Toast.makeText(SpinnerViewPractice.this, spinner_value, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(SpinnerViewPractice.this, "NothingSelected", Toast.LENGTH_SHORT).show();
}
});
}
}
As the documentation describes:
Callback method to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter becomes empty.
This means that the method is called whenever the currently selected item is removed from the list of available items. As the doc describes, this can occur under different circumstances, but generally if the adapter is modified such that the currently selected item is no longer available then the method will be called.
This method may be used so that you can set which item will be selected given that the previous item is no longer available. This is instead of letting the spinner automatically select the next item in the list.
From the doc here.
onNothingSelected is a Callback method to be invoked when the selection disappears from this
view. The selection can disappear for instance when touch is activated
or when the adapter becomes empty.
I think it pretty much answers your question. So if your spinner disappear for other reason except selecting the item then onNothingSelected will be called. So as it's name tells it is needed to find out when nothing is selected

Setting spinner position dynamically in android?

I am developing an app where i need to set spinner values dynamically based on previous screen values. My code...
Main.java.
String[] values = {"All","Only Walk-in","Only Phone","Only Web","Walkin-phone","Walkin-web","phone-web"};
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,apttypes);
spinner.setAdapter(adapter);*/
But here what i want is from previous screen i am getting some value (like spinner postion). based on that i need to set spinner value to display...
Means from previous screen if i got values =0 means,
i need to set spinner value to display "All" from values array at top.
If i got value= 5 means,
i want to set spinner value to display as "Walkin-web"
How can i do that. can anyone help me with this...
Pass the value in from the previous Activity using extras in the Intent you use to launch it. Then when you've read the value call
int position = getIntent().getIntExtra( "name", defaultValue );
spinner.setSelection( position );
Which will move the spinner to the index you selected.
Use following array code and create new array adapter each time
String[] str=new String[maxsize-4];
you can implement onItemClick event on Spinner like this and setSelection(position)
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
payfeeTabStudentNameSpinner.setSelection(position);
spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Inside your First Activity A.java
public static String Position = YOUR_SPINNER1.getSelectedItem().toString();
Inside your Second Activity B.java
if(A.Position.equals("0")){
//Set your adapter accordingly
}else if(A.Position.equals("1")){
//Set your adapter accordingly
}
You can assign the Spinner's Position using the following code..
Spinner s1;
-----------
-----------
int position=valueFromPrevious;
s1.setSelection(position);
-----------
-----------
pass the values from previous activity using Extras and then when u want use it in your current activity follow this:
If u get String value then typecast it into interger by parseInt method...
String strtext4 = getIntent().getStringExtra("qualification");
then
int position = Integer.parseInt(strtext4);
after this just set it to your spinner
qualificationspinner.setSelection(position);

Categories

Resources