I have used a List and an Arrayadapter to put strings in a Listview. Now I want to remove the selected Item. I have tried the following code, but it's not working well. How can I fix this?
Here's my code:
TextView t1;
String[] temp;
mylist = (ListView) findViewById(R.id.list);
final List<String> wordList = Arrays.asList(temp);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
wordList);
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int index = mylist.getSelectedItemPosition();
int count=mylist.getCount();
for(int i=0;i<=count;i++) {
if (index >= 0) {
t1.setText(wordList.remove(index));
}
adapter.notifyDataSetChanged();
}
}
});
Here's more source code:
public class Edit extends Activity {
SharedPreferences sharedpref;
ListView mylist;
String[] temp;
String name,
ArrayAdapter<String> adapter;
Button save, delete, cancel;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
mylist = (ListView) findViewById(R.id.list);
cancel = (Button) findViewById(R.id.cancel1);
save = (Button) findViewById(R.id.save1);
delete = (Button) findViewById(R.id.delete1);
t1=(TextView)findViewById(R.id.textView1);
sharedpref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
name = sharedpref.getString("Visible_selected", "");
String delimiter = "\n";
temp = name.split(delimiter);
mylist.setItemsCanFocus(true);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
final List<String> wordList = Arrays.asList(temp);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, wordList);
mylist.setAdapter(adapter);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mylist.setOnItemClickListener(new OnItemClickListener() {
private String getSelectedItemOfList;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = sortedList.get(arg2).getStr_movieParam();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here's my updates code:
enter code here
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
adapter.remove(getSelectedItemOfList);
adapter.notifyDataSetChanged();
}
});
mylist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
SparseBooleanArray checkedPositions =mylist.getCheckedItemPositions();
if(checkedPositions.get(arg2)==true){
getSelectedItemOfList = (String) mylist.getItemAtPosition(arg2);
t1.append(getSelectedItemOfList.toString()+"\n");}
}
});
Try to implement the onitemclickListener and get the item id and delete the item clicked in the arrayadapter and next call adapter.notifyDataSetChanged();
Try this...
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3) {
String value = (String) adapter.getItemAtPosition(position);
adapter.remove(value);
adapter.notifyDataSetChanged();
}
});
I think you should use OnItemClickListener() for the listview so that when you select/click any list item you will get name of selected item then you can perform deletion operation.
Try this.
mylist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos,
long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = mylist.get(pos).toString(); // here you will get selected item name.
}
}
Hope this will help you.
see this code which help you to understand how to remove items in listview.
private ArrayList<String> students = new ArrayList<>();
private ArrayAdapter<String> arrayAdapter;
private ListView myListView;
private int getSelectedIndex = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (ListView) findViewById(R.id.myListView);
students.add("rahim");
students.add("karim");
students.add("sumon");
students.add("rakib");
students.add("porag");
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, students);
myListView.setAdapter(arrayAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "hello " + students.get(position), Toast.LENGTH_LONG).show();
MainActivity.this.getSelectedIndex = position;
for (int i = 0; i < myListView.getChildCount(); i++) {
if(position == i ){
myListView.getChildAt(i).setBackgroundColor(Color.BLUE);
}else{
myListView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);//Menu Resource, Menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Toast.makeText(getApplicationContext(), "Item 1 Selected", Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
if(this.getSelectedIndex!=-1){
students.remove(this.getSelectedIndex);
this.getSelectedIndex = -1;
arrayAdapter.notifyDataSetChanged();
myListView.setAdapter(arrayAdapter);
}
return true;
default:
return false;
}
}
I would say the best way to do this (the really best way is a custom adapter) is to keep a copy of your arraylist of items in your class. Then when delete is called remofve the item form your copy of the arraylist and re-init the listview.
First thing i want to tell you that code you have written is wrong...
May be you want the context menu on listview..
if you are getting the position of selected item then you can have
adapter.removeItem(adapter.getItem(position));
adapter.notifyDatasetChanged()
may this help you
replace
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mylist.setOnItemClickListener(new OnItemClickListener() {
private String getSelectedItemOfList;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = sortedList.get(arg2).getStr_movieParam();
}
});
with
mylist.setOnItemClickListener(new OnItemClickListener() {
private String getSelectedItemOfList;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = sortedList.get(arg2).getStr_movieParam();
}
});
and
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
adapter.remove(getSelectedItemOfList );
adapter.notifydatasetChanged
}
});
check this one
Try working with this code in the http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/
public class ListTest extends Activity {
String selectedItem;
ArrayAdapter<String> adapter;
ArrayList<String> newList = new ArrayList<String>();
ListView l1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_test);
l1 = (ListView) findViewById(R.id.lsvSign);
newList.add("Android");
newList.add("iOS");
newList.add("Mac");
newList.add("Windows");
newList.add("Linux");;
Collections.sort(newList);
adapter = new new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,newList);
l1.setAdapter(adapter);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
selectedItem = l1.getItemAtPosition(position).toString();
RemoveItem(selectedItem);
}
});
protected void RemoveItem(String item) {
newList.remove(item);
adp2.notifyDataSetChanged();
}
}
}
Try working code: listAdapter.remove(listAdapter.getItem(position));
mainListView.setAdapter( listAdapter );
Related
I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});
}
public void valide_button() {
valideButton = (Button) findViewById(R.id.valide_button);
valideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestProjectActivity.this,
MetroMapActivity.class);
startActivity(intent);
}
});
}
}
The way to get the selection of the spinner is:
spinner1.getSelectedItemPosition();
Documentation reference:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.
So you could change that line to:
TestProjectActivity.this.number = position + 1;
If that does not fix the problem, please post the error message generated when your app crashes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.button);
spinner = findViewById(R.id.sp_item);
setInfo();
spinnerAdapter = new SpinnerAdapter(this, arrayList);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//first, we have to retrieve the item position as a string
// then, we can change string value into integer
String item_position = String.valueOf(position);
int positonInt = Integer.valueOf(item_position);
Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
note: the position of items is counted from 0.
final int[] positions=new int[2];
Spinner sp=findViewByID(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText( arg2....);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if (position ==0) {
if (rYes.isChecked()) {
Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
} else if (rNo.isChecked()) {
Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
}
}
This code is supposed to select both check boxes.
Is there a problem with it?
i want to take the value in spinner and user press the ok Button and then start new activity from against the value of spinner. The string array is just like that.
public class Menu extends Activity implements View.OnClickListener {
private String[] array_spinner = {"Select Here", "Honda", "Toyota", "Mitibushi"};
private String[] array_spinner01 = {"Select Here", "Civic", "Gli", "Lancer"};
// //private String[] array_spinner02={"1999","2000","2005"};
// Spinner s,s1,s2;
// Button ok;
//#Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
Spinner s, s1;
Button ok;
Object _globalString;
Object _globalString2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
s1 = (Spinner) findViewById(R.id.spinner01);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner01);
s1.setAdapter(adapter1);
//
// s2=(Spinner)findViewById(R.id.spinner02);
// ArrayAdapter<String> adapter2=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,array_spinner02);
// s2.setAdapter(adapter2);
ok = (Button) findViewById(R.id.btn_ok);
ok.setOnClickListener(this);
s.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
_globalString = parent.getItemAtPosition(position);
}
//#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
s1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
_globalString2 = parent.getItemAtPosition(position);
}
// #Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_ok) {
if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
startActivity(new Intent(getApplicationContext(), Civic.class));
} else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
startActivity(new Intent(getApplicationContext(), Toyota.class));
}
}
}
}
please tell me where is my mistake in this code because i stucked and i cant find it
There are many ways to do this, but I would recommend to follow these solution :
First :
save the value of the spinner in a global string variable by declaring a string at the top of your Activity like below :
public class Menu extends Activity implements View.OnClickListener {
Spinner s1, s2;
Button acceptbutton;
String _globalString ;
String _globalString2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Your code ..
Now you need to save the value of your selected field into that string, everytime your spinner changes value :
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
_globalString = parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
s.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
_globalString2 = parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Now you can switch activity based on spinner value :
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_ok) {
if (_globalString.equals("Honda") || _globalString2.equals("Civic")) {
startActivity(new Intent(getApplicationContext(), Civic.class));
} else if (_globalString.equals("Toyota") || _globalString2.equals("Gli")) {
startActivity(new Intent(getApplicationContext(), Toyota.class));
}
}
}
Im using a spinner and i was wondering how can you get an item from the spinner to appear in an image button the images are using string as value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_up);
spinnerListener();
St = (Spinner) findViewById(R.id.Namess);
imageButton01 = (ImageButton) findViewById(R.id.ImageButton1);
}
private void spinnerListener() {
ArrayList<Name> players = List_content.ENTRY_LIST_PLAYERS;
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(Line_up.this, android.R.layout.simple_spinner_item);
{for (Name p : players){adapter.add(p.f_name);}};
playerss.setAdapter(adapter);
playerss.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> av, View v,
int position, long itemId) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void goalkeeper(View v){
st.performClick();
}
OK. Here's the trick:
When ImageButton is clicked, you just need to call performClick() on the spinner object.
int[] pictureIds = {
R.drawable.picture1,
//add as many picture IDs as you want
R.drawable.picture2
}
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinner.performClick();
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
imageButton.setImageResource(pictureIds[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Don't forget to set spinner adapter just as you did correctly in your question.
How to get value in second spinner and third spinner after selected first spinner and calculate [to create a simple converter]
here the code, there is no error in my code but this coding not run perfectly, please help me guys..
public class MainActivity extends Activity{
private Spinner firstSpinner, secondSpinner, thirdSpinner;
private Button convert;
EditText input, output;
Double awal, numtemp;
String num, hasil;
ArrayList<String> ChooseType = new ArrayList<String>();
ArrayList<String> ChooseSuhu = new ArrayList<String>();
ArrayList<String> ChooseBerat = new ArrayList<String>();
ArrayList<String> ChoosePanjang = new ArrayList<String>();
ArrayList<String> ChooseData = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstSpinner = (Spinner) findViewById(R.id.ChooseType);
secondSpinner = (Spinner) findViewById(R.id.ChooseType1);
input = (EditText) findViewById(R.id.editText);
thirdSpinner = (Spinner) findViewById(R.id.ChooseType2);
output = (EditText) findViewById(R.id.editText1);
convert = (Button) findViewById(R.id.konBut);
fillarray();
firstSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line, ChooseType));
firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,long arg3) {
// TODO Auto-generated method stub
if (pos == 1) {
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseSuhu));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseSuhu));
}
/*else if(pos == 2){
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseBerat));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseBerat));
}
else if(pos == 3){
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChoosePanjang));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChoosePanjang));
}
else if(pos == 4){
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseData));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseData));
}*/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
convert.setOnClickListener(new convert());
}
public void onClickListener(){
secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
if(arg2 == 0){
num = input.getText().toString();
awal = Double.parseDouble(num);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
thirdSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
numtemp = awal;
hasil = numtemp.toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private class convert implements OnClickListener {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
output.setText(hasil);
}
}
private void fillarray() {
// TODO Auto-generated method stub
ChooseType.add("Choose");
ChooseType.add("Suhu");
ChooseType.add("Berat");
ChooseType.add("Panjang");
ChooseType.add("Data");
ChooseSuhu.add("celcius");
ChooseSuhu.add("reamur");
ChooseSuhu.add("fahrenheit");
ChooseSuhu.add("kelvin");
ChooseBerat.add("pon");
ChooseBerat.add("ton");
ChooseBerat.add("miligram");
ChooseBerat.add("gram");
ChooseBerat.add("kilogram");
ChooseBerat.add("oz");
ChoosePanjang.add("centimeter");
ChoosePanjang.add("feet");
ChoosePanjang.add("inche");
ChoosePanjang.add("kilometer");
ChoosePanjang.add("meter");
ChoosePanjang.add("mile");
ChoosePanjang.add("centimeter");
ChooseData.add("bit");
ChooseData.add("byte");
ChooseData.add("kilobit");
ChooseData.add("kilobyte");
ChooseData.add("megabit");
ChooseData.add("megabyte");
ChooseData.add("gigabit");
ChooseData.add("gigabyte");
ChooseData.add("terabit");
ChooseData.add("terabyte");
}
}
Try this
OtherSpinner.getSelectedItem().toString();
I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});
}
public void valide_button() {
valideButton = (Button) findViewById(R.id.valide_button);
valideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestProjectActivity.this,
MetroMapActivity.class);
startActivity(intent);
}
});
}
}
The way to get the selection of the spinner is:
spinner1.getSelectedItemPosition();
Documentation reference:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.
So you could change that line to:
TestProjectActivity.this.number = position + 1;
If that does not fix the problem, please post the error message generated when your app crashes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.button);
spinner = findViewById(R.id.sp_item);
setInfo();
spinnerAdapter = new SpinnerAdapter(this, arrayList);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//first, we have to retrieve the item position as a string
// then, we can change string value into integer
String item_position = String.valueOf(position);
int positonInt = Integer.valueOf(item_position);
Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
note: the position of items is counted from 0.
final int[] positions=new int[2];
Spinner sp=findViewByID(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText( arg2....);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if (position ==0) {
if (rYes.isChecked()) {
Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
} else if (rNo.isChecked()) {
Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
}
}
This code is supposed to select both check boxes.
Is there a problem with it?