I want the option selected in the spinner menu to be displayed in a textview whose id is 'spinnertxt'.
I'm able to open the spinner menu and select the option, but after that nothing happens.
Here is the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Addproject extends Activity implements OnItemSelectedListener {
protected int mPos;
protected String mSelection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addproject);
Spinner spinner = (Spinner) findViewById(R.id.difficultyspinner); ArrayAdapter<CharSequence>
adapter = ArrayAdapter.createFromResource(this,R.array.difficultyarray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_addproject, menu);
return true;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
Addproject.this.mPos = pos;
Addproject.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView spinnerresult = (TextView)findViewById(R.id.spinnertxt);
spinnerresult.setText(Addproject.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
//NOTHING TO LOOK HERE, MOVE ALONG
}
}
Can someone tell me why? I'm new to this.
Instantiate your array and use
Addproject.this.mSelection = array[pos];
instead of
Addproject.this.mSelection = parent.getItemAtPosition(pos).toString();
Solved!
A listener was missing.
I just added
spinner.setOnItemSelectedListener(this);
to onCreate() after setAdapater()
The listener is needed so that the application listens for taps on particular items.
Related
//MainActivity Details
package com.yay_or_nay.www.yayornayv1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Categories_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String sSelected=parent.getItemAtPosition(position).toString();
Toast.makeText(this,sSelected, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
XML file that i'm using
<?xml version="1.0" encoding="utf-8"?>
<string-array name="Categories_array">
<item>Technology</item>
<item>Science</item>
<item>Fashion</item>
<item>Photography</item>
<item>Cars</item>
<item>Lifestyle</item>
<item>Food</item>
</string-array>
for example, what do I add to my main activity when I click "Technology" spinner array button? so it can create a new activity action? I'm trying to make it so, if a user clicks an option from the drop-down list from the spinner, it'll create a new instance.
`
package project.com.spinnerclick;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity
private Spinner spinner;
List<String> categoriesArray;
Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUI();
}
private void initializeUI() {
//initialize context;
mContext = this;
//initialize list to pass it spinner adapter
categoriesArray = new ArrayList<>();
// assign values to categories list
categoriesArray = Arrays.asList(getResources().getStringArray(R.array.categories_array));
// initialize spinner component
spinner = (Spinner) findViewById(R.id.spinner);
//set spinner adapter
spinner.setAdapter(new ArrayAdapter<>(mContext,
android.R.layout.simple_spinner_dropdown_item, categoriesArray));
//setting in item select Listener to spinner items
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// you can open any activity by getting position of current
// selected item and open the related activity to this position
switch (position) {
case 1:
startActivity(new Intent(mContext, SecondActivity.class));
break;
case 2:
startActivity(new Intent(mContext, Main2Activity.class));
break;
}
//second solution by getting the clicked item value
// and open activity according this value
String selectItem = spinner.getItemAtPosition(position).toString();
Toast.makeText(mContext, selectItem, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
and here is spinner values in XML
<resources>
<string name="app_name">SpinnerClick</string>
<string-array name="categories_array">
<item>select Item</item>
<item>Technology</item>
<item>Science</item>
<item>Fashion</item>
<item>Photography</item>
<item>Cars</item>
<item>Lifestyle</item>
<item>Food</item>
</string-array>
</resources>
Use spinner.getSelectedItem.toString() instead of parent.getItemAtPosition….
my question is the following: I have a StartActivity, where the user can find a spinner and can choose from a list of options I have in an array. I want that the user can choose and then it jumps to another acitivity, for example the user chooses "Wien Simmering" and then the Simmering.java opens.
package at.co.ccc.mondel;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.os.Bundle;
public class StartingPoint extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, R.string.refreshed, Toast.LENGTH_SHORT).show();
break;
// action with ID action_settings was selected
case R.id.action_settings:
Toast.makeText(this, R.string.settings_clicked, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.starting_point, menu);
return true;
}
}
And here is the strings xml:
<string-array name="comboCasino">
<item>Wien Simmering</item>
<item>Wien Lugner City</item>
<item>Gmunden</item>
<item>Salzburg</item>
<item>Linz</item>
<item>Saalbach</item>
<item>Innsbruck</item>
<item>Reutte</item>
<item>Bregenz</item>
<item>Kufstein</item>
<item>Bratislava</item>
</string-array>
package at.co.ccc.mondel;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
public class StartingPoint extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
// this will be called when you select any item in this spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// get the text at that position
String Text = spinner.getSelectedItem().toString();
// if Text is equals to "Wien Simmering"
if(Text.equals("Wien Simmering")) {
// start a new activity
// here StartingPoint.this is context
// and simmering.class is the next activity going to be opened
Intent NewPost = new Intent(StartingPoint.this,Simmering.class);
startActivity(NewPost);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
} ); // i forget to add ); here that is why it was giving multiple markers error
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, R.string.refreshed, Toast.LENGTH_SHORT).show();
break;
// action with ID action_settings was selected
case R.id.action_settings:
Toast.makeText(this, R.string.settings_clicked, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.starting_point, menu);
return true;
}
}
so i want to make an app which loads a list with news.
now i did this to create a ListView and an ArrayAdapter:
package com.example.MPAK.newsapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.MPAK.newsapp.R;
import java.util.ArrayList;
public class NewsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
ListView lv = (ListView) findViewById(R.id.listView);
String[] values = new String[]{"News1", "NewsTitle2", "NewsTitle3", "NewsTitl4", "NewsTtitle5"};
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 1000; i++) {
list.add(values[i]);
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
the problem is it only shows me the 1st item from my values Array. I dont know why? What did i do wrong?
In the end i want the app to read xml files, read the title, display it on the List. By clicking on the list item it should open a new activity and display the description of the News.
Anyone has some good tutorial for that?
Thanks in advance
for (int i = 0; i < values.length; i++) {
list.add(values[i]);
}
for more informations about xml parsing there is a good tutorial presented by Darek Banas:
http://www.youtube.com/watch?v=92p6noZjRbk&index=9&list=PLGLfVvz_LVvQUjiCc8lUT9aO0GsWA4uNe
http://www.youtube.com/watch?v=Kt5wIEF7Lls&list=PLGLfVvz_LVvQUjiCc8lUT9aO0GsWA4uNe&index=10
http://www.youtube.com/watch?v=facoiyC7pC8&list=PLGLfVvz_LVvQUjiCc8lUT9aO0GsWA4uNe&index=11
http://www.youtube.com/watch?v=SlAHPyNvJ44&index=12&list=PLGLfVvz_LVvQUjiCc8lUT9aO0GsWA4uNe
and there is another good tutorial that is:
http://www.youtube.com/watch?v=40mYDQkK44A
I have an application that extends ActionBarActivity, but I'm using ListView and I need to implement OnItemClickListener and I'm not sure how to do this without extending ListActivity. Also I want to ask you: Moreover I have a button and I need to listen if the button is clicked or if an item in the list is clicked. I'm not pretty sure how to do this.
So I would really appreciate if you help me :)
Here is my code:
package com.src.vicnote;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button newButton;
String path = Environment.getExternalStorageDirectory().toString()+"/VICNote";
String lastOfPath;
File f = new File(path);
File files[] = f.listFiles();
String[] theNamesOfFiles = new String[files.length + 1];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theNamesOfFiles[0] = "Create new note";
for(int i = 1; i < theNamesOfFiles.length; i++) {
lastOfPath = files[i-1].toString().split("/")[files[i-1].toString().split("/").length-1];
theNamesOfFiles[i] = lastOfPath.replace(".txt","");
Log.d("Files", "String: " + theNamesOfFiles[i]);
}
ListView lv = (ListView) findViewById(R.id.notesList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
theNamesOfFiles);
lv.setAdapter(adapter);
newButton = (Button) findViewById(R.id.buttonNew);
newButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newNote = new Intent(MainActivity.this, NewNoteActivity.class);
startActivity(newNote);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can implement the OnItemClickListener interface and override onItemClick
lv.setAdapter(adapter);
lv.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this.this, "List View row Clicked at"+position,Toast.LENGTH_SHORT).show();
}
});
If the button belongs to activity_main.xml then what you have
newButton.setOnClickListener(new View.OnClickListener()
is right
You can set item click listener for ListView using this code.
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// it is used to get the clicked string
String theNamesOfFile = adapter.getItem(position);
}
});
I guess it can help you.
I searched for an answer but couldn't find it.
The problem:
Depending on the selected spinner-item the application should show a different image.
At this moment I can't get it to work.
The Url works like this: "my.site.com/images/" imc_met ".png"
were imc_met is the filename.
I can't get it to work.
Btw the app isn't finished yet
package example.myapplication;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
public class itemsActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
private Bitmap image;
private ImageView imageView;
private String imc_met, imc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(spinner1.getSelectedItem()!=null){
imc_met = spinner1.getSelectedItem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
imageView = (ImageView)findViewById(R.id.ImageView01);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
URL url = null;
try {
url = new URL("my.site.com"); //here should the right link appear.
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
if (url != null) {
image = BitmapFactory.decodeStream(url.openStream());
}
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(image);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
EDIT
Please post the whole code needed to be used to make the correct URL.
You call spinner1.setOnItemSelectedListener() twice! The first time (which should get the the item's value) is overwritten by the second time (which is CustomOnItemSelectedListener). Thus, the first listener won't be executed.
Remove the spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
(When you use setOnXxxListener(), only the last call will be used).
YOu should take one string object and on click of spinner item, assign it the value of it and then just use that in URL.
The problem here seems to be that your list values returned contains blank spaces in them which are not permissible in URLs.. Try something without spaces...
EDIT:- ok.. you havent set and OnItemSelectedListener on your spinner2 and have set 2 different ones on spinner1
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
And
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
You have set data on spinner2 and added listener on spiner1. spiner1 has no data. Add listener on spiner2 and run it again