I have two spinners, the first one shows the transport lines and the second one shows the stations of the line selected on the first spinner, the problem is that I want to set to empty value the second spinner when I click on the first one, but I can't do it.
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
I've tried a few options on the onItemClick() method of the first spinner but it's not working propertly:
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
protected void onPreExecute() {
// I'm trying all the options in this method
}
#Override
protected String[] doInBackground(String... strings) {
}
#Override
protected void onPostExecute(String[] result) {
}
}
});
Option 1:
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
Option 2:
spinner2.setAdapter(null);
Option 3:
ArrayList<String> list = new ArrayList<>();
list.add("");
spinner2.setSelection(list.size()-1);
Option 4:
arrayAdapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner2.setAdapter(arrayAdapter2);
spinner2.setSelection(arrayAdapter2.getCount());
spinner2.setOnItemSelectedListener(this);
arrayAdapter2.notifyDataSetChanged();
To sum up, there is a way to change the value of a spinner in function of the value of other one? Or just a way to change the value of a spinner to empty value?
Any help will be apreciated!
Use setOnItemSelectedListener instead of OnItemClickListener like:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos==0) // you can change condition as per your requirement
{
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
}
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
The complete code:
public class FillTransportPlaceActivity extends AppCompatActivity {
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
private ArrayList<String> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_transport_place);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner = findViewById(R.id.listLines);
spinner.setAdapter(arrayAdapter);
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner2 = findViewById(R.id.listStations);
spinner2.setAdapter(arrayAdapter2);
SharedPreferences sp = getApplicationContext().getSharedPreferences("transportButton", 0);
boolean metro = sp.getBoolean("metro", false);
boolean bus = sp.getBoolean("bus", false);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).toString() != null) {
String choice = parent.getItemAtPosition(position).toString();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
new stationsDB().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, choice);
else
new stationsDB().execute(choice);
}
}
});
}
private class stationsDB extends AsyncTask<String, String[], String[]> {
#Override
protected String[] doInBackground(String... strings) {
return DB_transportPlace.getStations(strings[0]);
}
#Override
protected void onPostExecute(String[] result) {
updateAdapter2(result);
}
}
public void updateAdapter(String[] result) {
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner.setAdapter(arrayAdapter);
}
public void updateAdapter2(String[] result) {
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner2.setAdapter(arrayAdapter2);
}
As I said I've tried with setOnItemSelectedListener instead of OnItemClickListener and it didn't work, only execute one time in the onCreate method, I just want to change the value of spinner2 to empty when I change the value of spinner1.
Related
I want to pass value from spinner to doInBackground in AsyncTask. I use this code:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, myArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(0);
OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Integer item = (Integer)parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
spinner.setOnItemSelectedListener(itemSelectedListener);
public void onClick(View view) {
SomeTask = new SomeTask();
SomeTask.execute();
class SomeTask extends AsyncTask<Void, Integer, Void> {
#Override
protected Void doInBackground (Void...params){
//do something with variable item
}
}
//do all next things
And I don't know how to pass variable item to doInBackground. Help me please and thank you.
I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable
i'm practising how to add an string to the addapter of the spinner. If i declare the string in the java activity, works perfectly, but if i do in the string.xml:
<string-array name="tabs">
<item>tab</item>
<item>tab1</item>
<item>tab2</item>
<item>tab3</item>
<item>tab4</item>
<item>tab5</item>
</string-array>
and in the java
ArrayAdapter<String> adapterr1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.tabs);
dont work.
what's the problem? Thanks
This is the full code, because i have problems whit the app (crash)
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs= getResources().getStringArray(R.array.tabs);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
String[] stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapterr1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
Edited 1
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The constructor ArrayAdapter<String>(Context, int, int) doesn't use the third argument as an array resource id; it uses it as the id of the TextView to use to display each item in the layout. Instead, you can pass getStringArray(R.array.tabs) as the third argument (or getContext().getStringArray(R.array.tabs), if this code is not in an Activity).
I have two spinners, A and B. The spinner options for B changes based on the option A selected.
How I am doing it:
final ObjA[] ObjAArray = // db call
SpinAdapter<ObjA> sObjAAdapter = new SpinAdapter<ObjA>(this, android.R.layout.simple_spinner_item, ObjAArray);
sObjA.setAdapter(sObjAAdapter);
sObjA.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// Here you get the current item (a User object) that is selected by its position
ObjA ObjA = ObjAArray[position];
final ObjB[] ObjBArray = // db call
SpinAdapter<ObjB> sObjBAdapter = new SpinAdapter<ObjB>(Activity.this,
android.R.layout.simple_spinner_item, ObjBArray);
sObjB.setAdapter(sObjBAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> adapter) { }
});
This works fine but I need to select A and B to a specific value in the beginning.
ObjB oldObjB = // db call
ObjA oldObjA = // db call
int ObjAIndexSpinner = Arrays.asList(ObjAArray).indexOf(oldObjA);
sObjA.setSelection(ObjAIndexSpinner);
final ObjB[] ObjBArray = DBObjB.getAllObjB(Activity.this, oldObjA.getID());
int ObjBIndexSpinner = Arrays.asList(ObjBArray).indexOf(oldObjB);
sObjB.setSelection(1);
The first spinner gets set properly but the second one doesn't. It defaults to the 0th index. How would I solve this?
Thanks!
I just made a test program, to find out what's going on ... The layout just contains two spinners sp1 and sp2 :
private final int START_SELECTION_SPINNER1 = 0;
private final int START_SELECTION_SPINNER2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner sp1 = (Spinner)findViewById(R.id.sp1);
final Spinner sp2 = (Spinner)findViewById(R.id.sp2);
List<String> sp1List = new ArrayList<String>();
sp1List.add("Selection 1 Choice 1");
sp1List.add("Selection 1 Choice 2");
final Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
map.put(0, new ArrayList<String>() {{
add("SubSelection 1 Choice 1");
add("SubSelection 1 Choice 2");
}});
map.put(1, new ArrayList<String>() {{
add("SubSelection 2 Choice 1");
add("SubSelection 2 Choice 2");
}});
final ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,sp1List);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,new ArrayList<String>());
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
sp2.setAdapter(adapter2);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
adapter2.clear();
adapter2.addAll(map.get(position));
adapter2.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
//DO YOUR STUFF
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
sp1.setSelection(START_SELECTION_SPINNER1);
handler.post(new Runnable() {
#Override
public void run() {
sp2.setSelection(START_SELECTION_SPINNER2);
}
});
}
});
}
The important thing are the last few lines where I have the handler post the setSelection(). Now it works :)
I have one autocompleteTextview, where i am loading a string array in to it.. so that i can search the data by typing the firstletter .Now i need to load a list on selecting each item in autocomplete textview .. How is it possble?
this is my autocompletetextview
autocomplete = (AutoCompleteTextView) customView.findViewById(R.id.myautocomplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line, ENTON_WEB);
autocomplete.setAdapter(adapter);
Thanks in advance
String s1="xcv";
String s2;
String [] search=new String[20];<---- this is your search list
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ac=(EditText)findViewById(R.id.editText1);
ListView lv=(ListView)findViewById(R.id.listView1);
//ac.setThreshold(3);
//l.add("pp");
s2=ac.getText().toString();
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
l);
//adapter.add("pp");
lv.setAdapter(adapter);
lv.setOnItemClickListener(mMessageClickedHandler);
new Thread(new Runnable() {
public void run() {
while(infra){
s2=ac.getText().toString();
if(s1!=s2 && s2.length()>=1){
s1=s2;
for(int=0;i<20;i++)
{
int index=search[i].index(s1);
if(index >-1)
{
lst.add(search[i])<----- lst is the arraylist of strings you should create
}
}
}
}
runOnUiThread(new Runnable() {
public void run() {
adapter.clear();
for(int z=0;z<k;z++){
adapter.add(s[z]);
}
adapter.notifyDataSetChanged();
}
});
}
}
}}).start();
}
private OnItemClickListener mMessageClickedHandler = new
OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
infra=false;
String s=d[position];
finish();
Intent k=new Intent(InfrapagesActivity.this,InfrapagesActivity.class);
startActivity(k);
Intent i=new Intent(this,nextactivity.class);
i.putExtra("hash",s);
startActivity(i);
}
};
}
next activity
public class nextactivity extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
type = getIntent().getExtras().get("hash").toString();....
you can search using the string "type" and populate the list...