Using spinner in android app - android

Can someone please help me. I have just started Android development and I want to create an app which has a drop down menu. There seems to be two errors in my code which I am unable to resole. The errors are at implements OnItemSelectedListner and at spinner.setOnItemSelectedListener(this);
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{
int Cups = 1;
int Price = 1;
int Sum = 0;
private Spinner spinner;
private static final String[]paths = {"item 1", "item 2", "item 3", "item 4", "item 5"};
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position)
{
case 0:
Price=0.5;
break;
case 1:
Price=1;
break;
case 2:
Price=2;
break;
case 3:
Price=3;
break;
case 4:
Price=4;
break;
}
}

The first Error is to implement the correct OnItemClickListener
implements AdapterView.OnItemClickListener
The second is to override the correct method from the listener which is
#Override
onItemClick(AdapterView<?> parent, View view, int position, long id) {
}

Related

Spinner performclick() not working

I want the spinner to open up by itself when I run the activity, so I'm using the performClick() method but it's showing this error:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
What should I do?
Here is my code
public class FacilityComplaint extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner1;
private static final String[] suggestions = {"Select from suggestions", "Switch not working",
"Switch faulty", "Switch light not working", "Switch handle faulty"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facility_complaint);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(FacilityComplaint.this,
android.R.layout.simple_spinner_item, suggestions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
spinner1.performClick();
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
//do something
break;
case 1:
//do something
break;
case 2:
//do something
break;
case 3:
//do something
break;
case 4:
//do something
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
For spinner you have don't need to implement "AdapterView.OnItemSelectedListener" Create like this .
spinner1 = (Spinner) findViewById(R.id. spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
suggestions, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.performClick();
// Get selecte index or Suggestions
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String selectedSugg = suggestions[pos];
Log.d("Suggestions ", selectedSugg);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

I can't set onItemSelectedListener in spinner array by 'for' Loop

I have nine spinners with same function, input value to int array.
So, I wrote code with 'for' Loop. I thought that's effective. But it didn't work.
When I execute this and check spinner, there's no change of int array.
How can I input value to int array with 'for' Loop?
//global
int num;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spinner[] spinner;
spinner = new Spinner[9];
int[] values;
values = new int[9];
for(int i = 0; i < 9; i++){
spinner[i] = new Spinner(this);
spinner[i] = (Spinner) findViewById(spinnerId[i]);
spinner[i].setAdapter(adapter);
values[i] = 0;
}
//OnItemSelectedListener
for(int i = 0; i < 9; i++){
num++;
spinner[i].setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
values[num] = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
Try this....
1. activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/spinner_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dynamic Spinner"
android:id="#+id/textView3"
android:textColor="#000000"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
</RelativeLayout>
2.MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout mContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GenerateView();
}
private void GenerateView() {
mContainer = (LinearLayout)findViewById(R.id.spinner_container);
LinearLayout.LayoutParams mRowLayoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
mRowLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
mRowLayoutParams.setMargins(8, 8, 8, 8);
String[] ITEMS = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"};
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ITEMS);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < 9; i++) {
Spinner spinner = new Spinner(this);
spinner.setId(i);
spinner.setAdapter(adapter0);
spinner.setOnItemSelectedListener(mOnItemSelectedListener);
spinner.setLayoutParams(mRowLayoutParams);
mContainer.addView(spinner);
}
}
private AdapterView.OnItemSelectedListener mOnItemSelectedListener = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
switch (spinner.getId()) {
case 0:
shpwToast(spinner.getSelectedItem().toString());
break;
case 1:
shpwToast(spinner.getSelectedItem().toString());
break;
case 3:
shpwToast(spinner.getSelectedItem().toString());
break;
case 4:
shpwToast(spinner.getSelectedItem().toString());
break;
case 5:
shpwToast(spinner.getSelectedItem().toString());
break;
case 6:
shpwToast(spinner.getSelectedItem().toString());
break;
case 7:
shpwToast(spinner.getSelectedItem().toString());
break;
case 8:
shpwToast(spinner.getSelectedItem().toString());
break;
case 9:
shpwToast(spinner.getSelectedItem().toString());
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
private void shpwToast(String content) {
Toast.makeText(MainActivity.this, "Item Selected : " + content, Toast.LENGTH_SHORT).show();
}
}
3. Result
The reason this is not working
values[num] = position;
The num value is 9 always when the event listener triggers.
You could create an inner class implementing the listener interface, with an index varible assigned on constructor, like this:
private class IndexedItemSelectedListener implements AdapterView.OnItemSelectedListener {
private final int index;
public IndexedItemSelectedListener(int index) {
this.index = index;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
values[index] = position;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
And use it like this:
//OnItemSelectedListener
for (int i = 0; i < spinner.length; i++) {
spinner[i].setOnItemSelectedListener(new IndexedItemSelectedListener(i));
}

how to disable other items in listview

I have a list view, this setup is like the guess that logo game where you can't proceed with the next level until you finish the level before that. I have been searching all night already, I found examples and questions about disabling other items in listview but I cannot find the specific answer that I can use in my problem. Now I am asking. how to disable other items in listview? and enable them if the level before them is complete, I'm also trying to achieve this by getting the "level status" from sqlite database.
This is the jave code extends ListActivity:
static final String[] LEVELS = new String[] { "Level 1", "Level 2", "Level 3",
"Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9", "Level 10" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_play, LEVELS));
final ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(
AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
switch(position) {
case 0:
Intent lvlOne = new Intent(".LevelOneActivity");
startActivity(lvlOne);
break;
case 1:
Intent lvlTwo = new Intent(".LevelTwoActivity");
startActivity(lvlTwo);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
default:
break;
}
}
});
}
I think that you should just make easy class which will hold your level and class which it should start
public class ListItemInfo
{
String mActivityToStart;
int mLevel
}
then just add a TAG to each of list items which you will hold in adapter when creating a view
after that
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(
AdapterView<?> parent, View v, int position, long id)
{
if (#DB GET UNLOCKED LVL) >= ((ListItemInfo)v.getTag()).mActivityToStart)
{
Intent lvlOne = new Intent(getContext(), ((ListItemInfo)v.getTag()).mLevel);
startActivity(lvlOne);
}
});

Spinner Switch Case Problem

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.

i've got some problems with my spinner

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.

Categories

Resources