So i'm starting a application and the first thing to do is make a description of a city when the city is selected. I can show the description but it make the description of all the cities on the same time and it don't come out when i select another city : it add more and more .
this is my code :
public class Main extends Activity implements OnItemClickListener, OnItemSelectedListener {
TextView description;
Spinner spin;
ArrayAdapter adapter_city;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
description = (TextView)findViewById(R.id.description);
spin = (Spinner)findViewById(R.id.spin);
adapter_city = ArrayAdapter.createFromResource(this, R.array.Cities, android.R.layout.simple_spinner_item);
adapter_city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_city);
spin.setOnItemSelectedListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
switch(position){
case 0 : description.append(getString(R.string.Paris));
case 1 : description.append(getString(R.string.Chicago));
case 2 : description.append(getString(R.string.NewYork));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
thank you .
Instead of:
description.append(getString(R.string.Paris));
Why don't you just use:
description.setText(R.string.Paris);
Here is what it should look like:
case 0 : description.setText(R.string.Paris);
break;
case 1 : description.setText(R.string.Chicago);
break;
case 2 : description.setText(R.string.NewYork);
break;
You need to add breaks after every case or execution will continue to all of them when matched:
switch(position){
case 0 : description.setText(R.string.Paris); break;
case 1 : description.setText(R.string.Chicago); break;
case 2 : description.setText(R.string.NewYork); break;
}
Try using description.setText(CharSequence text) instead of append. Append is for appending text.
Related
I have multiple AutoCompleteTextView in layout, so i've implemented AdapterView.OnItemClickListener globally.
Now issue is that,
i can't compare AutoCompleteTextView using switch case inside listener,
Code
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (v.getId()) {
case R.id.ac_education:
s_ac_education = testArray[position];
break;
case R.id.ac_ethincity:
s_ac_ethincity = testArray[position];
break;
case R.id.ac_languages:
s_ac_languages = testArray[position];
break;
case R.id.ac_location:
s_ac_location = testArray[position];
break;
case R.id.ac_religion:
s_ac_religion = testArray[position];
break;
case R.id.ac_travel:
s_ac_travel = testArray[position];
break;
}
}
Note: view.getId() always returning same value,
i've also tried if-else but that doesn't work also.
(I've already wasted couple of hours)
EDIT
private void init(View v) {
ac_languages = v.findViewById(R.id.ac_languages);
ac_religion = v.findViewById(R.id.ac_religion);
ac_location = v.findViewById(R.id.ac_location);
ac_travel = v.findViewById(R.id.ac_travel);
ac_ethincity = v.findViewById(R.id.ac_ethincity);
ac_education = v.findViewById(R.id.ac_education);
ptr.setACTVAdapter(getActivity(), ac_education, ac_ethincity, ac_languages, ac_location, ac_religion, ac_travel);
ac_languages.setHint(R.string.languages);
ac_religion.setOnItemClickListener(this);
ac_religion.setHint(R.string.religion);
ac_location.setOnItemClickListener(this);
ac_location.setHint(R.string.where_do_you_live);
ac_travel.setOnItemClickListener(this);
ac_travel.setHint(R.string.where_can_you_travel);
ac_ethincity.setOnItemClickListener(this);
ac_ethincity.setHint(R.string.ethincity);
ac_education.setOnItemClickListener(this);
ac_education.setHint(R.string.education);
}
setAdapter(is defined in another class)
public void setACTVAdapter(Activity activity, AutoCompleteTextView... actv) {
testArray = activity.getResources().getStringArray(R.array.testArray);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, testArray);
for (AutoCompleteTextView ac : actv) {
ac.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
Thanks
First, create your custom onItemClickListener:
public class MyClickListener implements AdapterView.OnItemClickListener {
AutoCompleteTextView ac;
public MyClickListener(AutoCompleteTextView myAc){
ac = myAc;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch(ac.getId()){
case R.id.autocomplete_1:
//your code
break;
case R.id.autocomplete_2:
//your code
break;
}
}
}
Use it for your AutoCompleteTextView:
autoCompleteTextView.setOnItemClickListener(new MyClickListener(autoCompleteTextView));
You should not rely on view when you're working with a list, specially when you're setting a general item click listener.
Instead get an instance to each view by their id on public View getView(int position, View convertView, ViewGroup parent) method of your AutoCompleteTextView adapter and declare a click listener to work with the position you got there, if you want to set multiple click listeners on a row individually.
For example:
public class ExampleAdapter extends BaseAdapter {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView.findViewById(R.id.ac_education);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
}
...
}
According to official doc for AdapterView.OnItemClickListener,
onItemClick() method have following parameters,
parent: The AdapterView where the click happened.
view: The view within the AdapterView that was clicked (this will be a
view provided by the adapter)
position: The position of the view in the adapter.
id: The row id of the item that was clicked.
So, you might want to use parent instead of view, to receive id of that parent AutoCompleteTextView.
(Check this answer to get more details.)
It's not the child that you want to track it's clicks, it's convertView.
So, if you really want to handle child clicks outside of your adapter, you can do like this:
public interface OnViewClickListener {
void onViewClick(View view, int position);
}
Then inside of your adapter:
private OnViewClickListener mListener;
public void setOnViewClickListener(final OnViewClickListener listener) {
mListener = listener;
}
And for every child you want:
child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
if (mListener != null) {
mListener.onViewClick(v, position);
}
}
});
And with this you can get item clicks:
adapter.setOnViewClickListener(new OnViewClickListener() {
#Override
public void onViewClick(final View view, final int position) {
** PUT YOUR SWITCH CASE HERE! **
}
});
First setup data into Pojo class using array list. And return as string for autotextview. Match that string with arraylist's item value using for loop.
autoPartyName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
for (CustomersModel list : customersList) {
if(list.toString().equals(adapterView.getItemAtPosition(position))){
etContactPhone.setText(list.getContact());
etContactAddress.setText(list.getAddress());
}
}
}
});
i'm trying to make my spinner shows value depends on what it selected before. but it for the second time because i did it in the first time it shows depend on selected item before but it's not working when i try it in the second time depends on what it selected from the first time selected before.
this is my code
gedungSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
if (position == 0) {
LantaiSpinnerRektorat();
lantaiSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0 : RuanganLantai1Rekto();
case 1 : RuanganLantai2Rekto();
case 2 : RuanganLantai3Rekto();
case 3 : RuanganLantai4Rekto();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
} else if (position == 1) {
LantaiSpinnerGL();
lantaiSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i){
case 0 : RuanganLantai1GL();
case 1 : RuanganLantai2GL();
case 2 : RuanganLantai3GL();
case 3 : RuanganLantai4GL();
case 4 : RuanganLantai5GL();
case 5 : RuanganLantai6GL();
case 6 : RuanganLantai7GL();
case 7 : RuanganLantai8GL();
case 8 : RuanganLantai9GL();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
}
});
please help me to make it work. it's not a duplicate question because i want to make the spinner depends on item selected for the second time item selected before. the code i have tried in swich case statement.
it not shows any error
It is not a right to put a listener for item1 inside a listener for item2, so separate the 2 listeners from each other. create a variable gedungSelectedItemPosition which will only hold 0 or 1 based on the first spinner's selection. Then OnItemSelectedListener of the second spinner, check whether gedungSelectedItemPosition is 0 or 1 before processing your selection. Check my code below:
int gedungSelectedItemPosition = -1; //initializing it with a value not 0 or 1
gedungSpn = findViewById(R.id.your_spinner_id);
lantaiSpn = findViewById(R.id.your_spinner_id);
gedungSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
gedungSelectedItemPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
}
});
lantaiSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (gedungSelectedItemPosition == 0) {
LantaiSpinnerRektorat();
switch (i) {
case 0 : RuanganLantai1Rekto();
case 1 : RuanganLantai2Rekto();
case 2 : RuanganLantai3Rekto();
case 3 : RuanganLantai4Rekto();
}
}
else if(gedungSelectedItemPosition == 1){
LantaiSpinnerGL();
switch (i){
case 0 : RuanganLantai1GL();
case 1 : RuanganLantai2GL();
case 2 : RuanganLantai3GL();
case 3 : RuanganLantai4GL();
case 4 : RuanganLantai5GL();
case 5 : RuanganLantai6GL();
case 6 : RuanganLantai7GL();
case 7 : RuanganLantai8GL();
case 8 : RuanganLantai9GL();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I have 2 arraylists, 1 is use to display the elements to the spinner and another one is use to display on a textview when one of the element from spinner is selected.
Example:
0---a---football
1---b---badminton
2---c---basketball
"a,b,c" are the elements in arraylist1; "football, badminton, basketball" are the elements in arraylist2; "0,1,2" are the index for both arraylists.The index of elements on both of the arraylists has already arranged properly as shown above.
What I want to do now is to let the spinner to display "a,b,c". When I select "b" in the spinner, the textview will show me "badminton".
What should I write in the onItemSelected of the spinner?Any idea for this?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
switch(position){
case 0:
textView1.setText(sportsList.get(0));
break;
case 1:
textView2.setText(sportsList.get(1));
break;
case 2:
textView3.setText(sportsList.get(2));
break;
}
}
});
You can get the selected item in the spinner by this code
String selected = spinner1.getText().toString();
Then check for the condition,
if(selected.equals("a")){
textview1.setText(array2.get(0));
}else if(selected.equals("b")){
......
try this,
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Textview your_tv = (Textview)findviewbyid(R.id.tv);
your_tv.setText(your_array[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
i want a spinner when select an item from the spinner its should load the corresponding java page that has been set.can we load a java page on selecting an item from spinner in android if so how can we implement this can any one provide some sample code
You can
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
switch(position) {
//Use cases to set Intents
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do Nothing
}
});
String[] items ={“One”,“Two”,“Three”,“Four”,“Five”};
Spinner sp = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item,items);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
//call first class
break;
case 1:
//call second class
break;
case 2:
//call third class
break;
case 3:
//call fourth class
break;
default:
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//nothing selected from spinner
}
});
just use spinner.onItemSelectedListener(new OnItemSelectedListener())
and in onItemSelected(AdapterView adapterview, View view, int position, long id) method body write your logic to start new activity based on the position.
EDIT: i have added in all of my code (excluding package and imports.....) and if i try to run it it crashes...... any ideas why?
public class BaseConverter extends Activity {
/** Called when the activity is first created. */
int inputBase;
int outputBase;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner input_spinner = (Spinner) findViewById(R.id.InputSpinner);
Spinner output_spinner = (Spinner) findViewById(R.id.OutputSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.base_numbers_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
input_spinner.setAdapter(adapter);
output_spinner.setAdapter(adapter);
input_spinner.setOnItemSelectedListener(new InputItemSelectedListener());
output_spinner.setOnItemSelectedListener(new OutputItemSelectedListener());
}
public class InputItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id)
{
/* switch (Integer.parseInt(parent.getItemAtPosition(pos).toString())
case ((Integer)parent.getItemAtPosition(pos)).intValue();
inputBase = 2;
break;
case 8:
inputBase = 8;
break;
case 10;
inputBase = 10;
break;
case 16;
inputBase = 16;
break;
*/
Toast.makeText(parent.getContext(), "You selected input base " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
public class OutputItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "You selected output base " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
and now need to have a switch - case scenario that all revolves around what the value they selected it. they are all numbers (the choices) and are stored in an Integer array. How do i set up that switch-case correctly? i tried doing a simple thing like
case ((Integer.parseInt(parent.getItemAtPosition(pos).toString())
So i figured it out. you need to make the array a STRING array and the use:
ArrayAdapter adapter = new ArrayAdapter
etc.....
then use
Integer.parseInt(parent.getItemAtPosition(position).toString());
to find the numerical value of whatever you selected. NOTE: it must be all numerical or it will give you an error.