i use Retrofit,
item shown but i can't selected.
after clicking item, toast and spinner not show anything (i can't select item on spinner).......
private List<String> spinnerItem;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
spinnerItem = new ArrayList<>();
setSpinner();
}
public void displayCities(List<CityResponse.City> cities) {
mainBinding.helloText.setText(cities.get(508).getNama());
for (CityResponse.City city : cities) {
Log.d(TAG, city.getNama());
spinnerItem.add(city.getNama());
}
}
private void setSpinner(){
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, spinnerItem);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mainBinding.spinnerCity.setAdapter(adapter);
mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "City : " + citySelected, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(CityActivity.this, "Not Selected", Toast.LENGTH_LONG).show();
}
});
}
Current solution:
You set the adapter in the setSpinner() with a list called spinnerItem which is empty for this moment. Then in displayCities() you fill list with values but forget to notify the adapter about new values added.
public void displayCities(List<CityResponse.City> cities) {
for (CityResponse.City city : cities) {
spinnerItem.add(city.getNama());
}
// 2. notify adapter that you have inserted new cities to the list
}
private void setSpinner(){
// 1. set the adapter with spinnerItem which is empty here
ArrayAdapter<String> adapter = new ArrayAdapter<>(..., ..., spinnerItem);
mainBinding.spinnerCity.setAdapter(adapter);
...
}
If adapter is local variable for setSpinner() then displayCities() does not have access to the adapter. Create adapter as global and then you can call adapter.notiftDataSetChanged() in point 2 comment
Your setSpinner() method:
private void setSpinner(final Context context, Spinner spinnerCity, List<Object> spinnerItem) {
final ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, spinnerItem);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = parent.getItemAtPosition(position).toString();
//adapter.notifyDataSetChanged();
Toast.makeText(context, "City : " + citySelected, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});
}
CityActivity.java:
Spinner spinnerCity = findViewById(R.id.spinner);
String[] spinnerItem = {"A", "B", "C", "D", "E"};
setSpinner(CityActivity.this, spinnerCity, Arrays.<Object>asList(spinnerItem));
Related
String[] categories = {
"All",
"Surgeon",
"Pediatrician",
"Cardiologist",
"Dermatologist"
};
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, categories);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
drspin.setAdapter(aa);
drspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
String a = drspin.getSelectedItem().toString();
sd.setText(a);
}
#Override
public void onNothingSelected(AdapterView << ? > parent) {
}
});
I have created a spinner having categories, and now I want to add the doctor's name in the subcategories, but I am not able to do it.
How to get id From JSON in spinner ?
i want get city id, if i choose item in spinner
example : https://imgur.com/shvfvnO,
if i choose "ACEH BARAT", i'll get id "512"
public void displayCities(List<CityResponse.City> cities) {
for (CityResponse.City city : cities) {
Log.d(TAG, city.getNama());
spinnerItem.add(city.getNama());
adapter.notifyDataSetChanged();
}
}
private void setSpinner(Context context){
adapter = new ArrayAdapter<>(context, R.layout.spinner_item, spinnerItem);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mainBinding.spinnerCity.setAdapter(adapter);
mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = parent.getItemAtPosition(position).toString();
Toast.makeText(context, "City : " + citySelected, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});
}
You need to create another arraylist to store id
locationId.add(i, response.getData());
now you need to get Id in spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
try {
selectedId = (String) locationId.get(position);
} catch (Exception e) {
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
You need to update mainBinding.spinnerCity.setOnItemSelectedListener() inside setSpinnner() method as I have added below:
private void setSpinner(Context context){
adapter = new ArrayAdapter<>(context, R.layout.spinner_item, spinnerItem);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mainBinding.spinnerCity.setAdapter(adapter);
mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Comment by Hari: You have already position available here so why you are doing this..
//String citySelected = parent.getItemAtPosition(position).toString();
/*Comment by Hari: If CityModel is the model of your cities ArrayList then it can be received inside spinner from the same index position as of the item as you are creating spinnerItemList in loop and both the list item position will be synced. */
CityModel cityModel = cities.get(position);
String citySelected = cityModel.getCity(); //Where cities is the main array in your activity class.
String cityId = cityModel.getCityId();
Toast.makeText(context, "City Name: " + citySelected + " // City Id: cityId" + , Toast.LENGTH_LONG).show();
//Do what you want to do next with the cityId here....
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});
}
Change your onItemSelected like this
mainBinding.spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String citySelected = parent.getItemAtPosition(position).toString();
for (CityResponse.City city : cities) {
if(city.getNama().equalsIgnoreCase(citySelected){
String city_id = city.getCityId(); //THIS IS YOUR CITY ID
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(context, "Not Selected", Toast.LENGTH_LONG).show();
}
});
I have created a spinner and it takes it's values from some database. The spinner also has a setOnItemSelectedListener which executes a method. Whenever I click on an item on the spinner i doesn't execute what's inside of it.
//Getting values from database
myDBJ = new JobDatabaseHelper(this);
ArrayList<String> jobname= myDBJ.getalljobname();
Spinner spinnerJob = (Spinner) findViewById(R.id.spinnerJob);
ArrayAdapter<String> adapterForJob = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, jobname);
adapterForJob.notifyDataSetChanged();
adapterForJob.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerJob.setAdapter(adapterForJob);
spinnerJob.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(InfoActivity.this, "test" + i ,Toast.LENGTH_SHORT).show();
//refreshrate();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I have even made a toast message inside of it so that I will know if it worked. When i click on an item it doesn't show the toast message. Another detail I would like to add is that this inside a method called "refreshspinners" which is called in my onCreate and my onPostResume override methods.
Can you try with a static String list:
ArrayList<String> jobname = (ArrayList<String>) Arrays.asList(new String[] {"Engineer", "Manager", "Business developer"});
Spinner spinnerJob = (Spinner) findViewById(R.id.spinnerJob);
ArrayAdapter<String> adapterForJob = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, jobname);
adapterForJob.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerJob.setAdapter(adapterForJob);
spinnerJob.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(InfoActivity.this, "test" + i ,Toast.LENGTH_SHORT).show();
//refreshrate();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
You may try like this:
Spinner sp = (Spinner) findViewById(R.id.spnner);
ArrayList<String> list = myDBJ.getAllJobName();
sp.setAdapter(new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,list));
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
{
Toast.makeText(getApplicationContext(),list.get(p3),Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> p1)
{
// TODO: Implement this method
}
});
I am new in android, for practise i took 4 spinners and 1 button. Spinner 1 value decide spinner 2 values and so on and i want to show some toast on click on button with all spinners values.
If value of all spinners are default then button will not highlight , whenever i give the valid value in all spinners button will be highlighted automatically.
Help me out, what should i do to get this point ??
Here is my code........
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
country = String.valueOf(sp1.getSelectedItem());
if (country.equals("USA")) {
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, states_usa);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter1);
}else if (country.equals("India")){
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, states_india);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter1);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
state = String.valueOf(sp2.getSelectedItem());
if (state.equalsIgnoreCase("California")) {
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, cities_california);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter2);
} else if (state.equals("Rajasthan")) {
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, cities_rajasthan);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter2);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
city = String.valueOf(sp3.getSelectedItem());
ArrayAdapter<String> adapter3;
switch (city) {
case "Los Angeles":
adapter3 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, areas_losangeles);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp4.setAdapter(adapter3);
break;
case "San Francisco":
adapter3 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, areas_sanfrancisco);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp4.setAdapter(adapter3);
break;
case "Jaipur":
adapter3 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, areas_jaipur);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp4.setAdapter(adapter3);
break;
case "Ajmer":
adapter3 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, areas_ajmer);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp4.setAdapter(adapter3);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
resultButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!sp1.getSelectedItem().toString().equals("Country") && !sp2.getSelectedItem().toString().equals("State")
&& !sp3.getSelectedItem().toString().equals("City") && !sp4.getSelectedItem().toString().equals("Area"))
{
Toast.makeText(getApplicationContext(),"Country : "+sp1.getSelectedItem().toString()+"\n"+
"State : "+ sp2.getSelectedItem().toString()+"\n"+
"City : "+sp3.getSelectedItem().toString()+"\n"+
"Area : "+sp4.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
} else {
resultButton.setEnabled(false);
}
}
});
Customize the spinner adapter with custom layout. Use TextView in the layout and set error on tap of button click listener.
button.setOnClickListener{
TextView spinnerText= (TextView)yourSpinner.getSelectedView();
spinnerText.setError("");
spinnerText.setTextColor(Color.RED);//It will just to highlight that this is an error
spinnerText.setText("your actual error text");
}
I hope it works!
I have created a List View containing-
"Baby Items"
"Bakery"
"Baking"
"Beverages"
"Canned Goods"
"Cereal Breakfast"
"Condiments"
"Dairy"
"Frozen Foods"
"Miscellaneous"
"Non-Food Items"
"Pasta / Rice"
"Snacks"
Then, I want to create an onClick layout to each list view also.
How can I use intent for the Baby_Items layout if I click on the Baby Items in Shopping Items (ListView)
Shopping Items (JAVA)
public class ShoppingItems extends ActionBarActivity {
ListView listView;
ArrayAdapter<String> adapter;
String[] item_category = {"Baby Items", "Bakery", "Baking",
"Beverages", "Canned Goods", "Cereal Breakfast", "Condiments",
"Dairy", "Frozen Foods", "Miscellaneous", "Non-Food Items",
"Pasta / Rice", "Snacks"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_items);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item_category);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+ " is selected",Toast.LENGTH_LONG).show();
}
});
}
BABY_ITEMS (JAVA)
public class Baby_Items extends ActionBarActivity {
ArrayList<Products> products = new ArrayList<Products>();
ListAdapter boxAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baby__items);
ListView lvMain = (ListView) findViewById(R.id.lvbabyitems);
lvMain.setAdapter(boxAdapter);
}
void fillData() {
for (int i = 1; i <= 20; i++) {
products.add(new Products("Products " + i, i * 100, false));
}
}
public void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
for (Products p : boxAdapter.getBox()) {
if (p.box){
result += "\n" + p.name;
totalAmount+=p.price;
}
}
Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
}
Check this tutorial for understanding basics about ListView implementation.You can use Intent for starting new Activity on ListView item click.
try this in onClick Method
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+ " is selected",Toast.LENGTH_LONG).show();
Intent intent = new Intent(ShoppingItems.this, Baby_Items.class);
intent.putExtra("baby_item", "" + parent.getItemAtPosition(position));
startActivity(intent);
}
});
and in Baby_Items activty get extra data like
if (getIntent().getExtras().containsKey("baby_item")) {
String item = getIntent().getStringExtra("baby_item");
}