How to use onclicklistener - android

I am trying to use an onclick listener on radio buttons to update a spinner I have. The default numbers in the spinner will be 15, 25, 35, and 45. When you select the KG radio button I want it to change to 8, 15, and 20. I have the following:
public class PlateCalc_Tab extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.platecalc_tab);
RadioButton weightSettingLB = (RadioButton)findViewById(R.id.weightSettingLB);
RadioButton weightSettingKG = (RadioButton)findViewById(R.id.weightSettingKG);
weightSettingLB.setChecked(true);
weightSettingLB.setOnClickListener(updateToLB);
weightSettingKG.setOnClickListener(updateToKG);
List<String> weightOfBarArray = new ArrayList<String>();
weightOfBarArray.add("15");
weightOfBarArray.add("25");
weightOfBarArray.add("35");
weightOfBarArray.add("45");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner weightOfBarSpinner = (Spinner) findViewById(R.id.weightOfBarSpinner);
weightOfBarSpinner.setAdapter(adapter);
}
View.OnClickListener updateToKG = new View.OnClickListener()
{
public void onClick(View view)
{
}
};
This code currently works. When I enter the following code into my onclick listener I get an error.
View.OnClickListener updateToKG = new View.OnClickListener()
{
public void onClick(View view)
{
List<String> weightOfBarArray = new ArrayList<String>();
weightOfBarArray.add("8");
weightOfBarArray.add("15");
weightOfBarArray.add("20");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner weightOfBarSpinner = (Spinner) findViewById(R.id.weightOfBarSpinner);
weightOfBarSpinner.setAdapter(adapter);
}
};
I get "Cannot Resolve Constructor" # this line:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);

Replace the array adapter initialization like below.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(PlateCalc_Tab.this, android.R.layout.simple_spinner_item, weightOfBarArray);

use
ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_spinner_item, weightOfBarArray);

Related

how to populate spinner options programmatically(not using xml)

I wish to set spinner items using program code and not using android:entries in xml layout.
But i am failing to do so.
I want to set spinner items according to a condition so cannot set it using xml statically so kindly help me to set dynamically.
here's the code:
public class Converter extends AppCompatActivity {
EditText et2;
TextView tv;
Spinner spr2,spr3;
ArrayAdapter adap2,adap3;
String []spr_2;
String []spr_3;
Button btn2;
String s1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_converter);
et2=(EditText) findViewById(R.id.et2);
tv=(TextView) findViewById(R.id.tv);
spr2=(Spinner) findViewById(R.id.spr2);
spr3=(Spinner) findViewById(R.id.spr3);
btn2=(Button) findViewById(R.id.btn2);
Bundle b=getIntent().getExtras();
et2.setText(b.getString("Value1"));
s1=b.getString("SpinnerValue");
adap2=adap3=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line);
if(s1 == "height") {
spr_2 = spr_3 = getResources().getStringArray(R.array.height);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "weight") {
spr_2 = spr_3 = getResources().getStringArray(R.array.weight);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "distance") {
spr_2 = spr_3 = getResources().getStringArray(R.array.distance);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "currency") {
spr_2 = spr_3 = getResources().getStringArray(R.array.height);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
}
}
Looks like code in if-blocks does not invoked. You shouldn't check objects equality like this. Operator '==' checks identity, not equality. Not s1 == s2, but s1.equals(s2), or even better Objects.equals(s1, s2).
Your code should look like this:
String[] items;
if ("height".equals(s))
items = getResources().getStringArray(R.array.height);
else if ("width".equals(s))
items = getResources().getStringArray(R.array.width);
....
else
items = getResources().getStringArray(R.array.width); //just for example
spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, items));

Change the string names in spinner dynamically from previous spinner chosen item

I want to change the string names in spinner dynamically android from previous spinner chosen item.
This is my Activity class:
public class SelectionActivity extends AppCompatActivity implements View.OnClickListener {
final String LOG = "Selection";
private Spinner spBranch;
private Spinner spSection;
private Spinner spSemester,spSubject;
private Button btnSend;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spBranch = (Spinner) findViewById(R.id.spBranch1);
spSection = (Spinner) findViewById(R.id.spSection1);
spSemester = (Spinner) findViewById(R.id.spSemester1);
spSubject=(Spinner) findViewById(R.id.spSubject1);
String[] items1 = new String[]{"CSE", "EEE", "EE", "ECE", "MECH", "CIVIL"};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items1);
spBranch.setAdapter(adapter1);
String[] items2 = new String[]{"A", "B", "C", "D", "E", "F"};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items2);
spSection.setAdapter(adapter2);
String[] items3 = new String[]{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th"};
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items3);
spSemester.setAdapter(adapter3);
String[] items4 = new String[]{"Math 1", "Programming in C", "Thermodynamics", "Communication English", "Physics", "Basic Electronics"};
String[] items5 = new String[]{"Chemistry", "Data Structure", "Mechanics", "Buiseness English", "Basic Electrical Engineering l ", "Math 2"};
if(spSemester.getSelectedItem().toString().equals("1st"))
{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items4);
spSubject.setAdapter(adapter4);
}
else{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items5);
spSubject.setAdapter(adapter4);
}
btnSend = (Button) findViewById(R.id.btnSend);
}
#Override
public void onClick(View v) {
Intent in = new Intent(SelectionActivity.this, ListActivity.class);
startActivity(in);
HashMap postData = new HashMap();
postData.put("txtBranch", spBranch.getSelectedItem().toString());
postData.put("txtSection", spSection.getSelectedItem().toString());
postData.put("txtSemester", spSemester.getSelectedItem().toString());
}
}
I am getting Error at r this line spSubject.setAdapter(adapter4);
New to andorid, so is there any other way to do this
Note that all of the code you have posted will run immediately when the Activity is created. This is the whole point of the onCreate() method. Nothing will happen when the user selects a new item in any of the Spinners. If you want the Spinners to change more dynamically, you need to add appropriate event listeners. In particular, you need to implement AdapterView.OnItemSelectedListener. An example is shown here. I suggest creating separate classes which implement the listener interface rather than implementing it on your activity class.
Try this code to know if first index in spSemester spinner is selected or not
if(spSemester.getSelectedItemPosition() == 1)
{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items4);
spSubject.setAdapter(adapter4);
}
else{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items5);
spSubject.setAdapter(adapter4);
}

android - how to set and get value from spinner

I am trying to set and get value to a spinner for item dynamically ?
any ideas ?
I just need help with the Spinner behavior right now, the rest should be quite easy.
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add("value");
spinnerAdapter.notifyDataSetChanged();
spinner.setSelection(0);
String text = spinner.getSelectedItem().toString();
1
2
3
XML file:
<Spinner android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Java file:
public class SpinnerExample extends Activity {
private String[] arraySpinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.arraySpinner = new String[] {
"1", "2", "3", "4", "5"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);
}
// To get value from spenner
spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView parent) {
}
});
}
Try this this will help you.
Spinner mySpinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(myAdapter);
If you want to add the elements dynamically, you can by doing this:
myAdapter.add("newelement");
myAdapter.notifyDataSetChanged();
Spinner spinn = findViewById(R.id.socialmedia_spinner_adsocmeda);
ArrayList<String> fam = new ArrayList<>();
fam.add("INSTAGRAM");
fam.add("FACEBOOK");
fam.add("GOODWALL");
fam.add("TWITTER");
fam.add("YOUTUBE");
fam.add("LINKEDIN");
fam.add("SNAPCHAT");
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (ProfileActivity.this,
android.R.layout.simple_list_item_1, fam.toArray(new String[0]));
spinn.setAdapter(myAdapter);
That's a way we can add data to spinner.

Spinner didnt update

public class MainActivity extends Activity {
private GestureDetector gestureDetector;
String dateData;
int choice ;
public HashSet<String> keyList = new HashSet<String>();
public ArrayList<String> temperatures = new ArrayList<String>();
public ArrayList<String> time = new ArrayList<String>();
public ArrayList<String> atList=new ArrayList<String>();
public ArrayList dataList=new ArrayList();
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this,new SwipeGestureDetector());
Spinner toList = (Spinner) findViewById(R.id.toList);
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
........................
protected void onPostExecute(String result)
{
Dialog.dismiss();
Iterator<String> iter = keyList.iterator();
while(iter.hasNext())
{
String key =iter.next();
dataAdapter.add(key);
dataAdapter.notifyDataSetChanged();
}
......................
..............................................................................
Sorry for this silly question , but i hav been doing it again and again from 0. it still doesn't get what i want. WHat problem i having now is bout the spinner didn't update itself after i set dataAdapter.notifyDataSetChanged().
Before this it work but my table isn't working .
now my table is working, this spinner not working. Omg
really need help desperately.
It seems that you are trying to add toList.setAdapter(dataAdapter) before initialize dataAdapter.So set dataAdapter after getting data in it,i.e. change
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
to
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());

Create a spinner programmatically in Android

I want to create a spinner without using XML. I am new in android and my knowledge is limited. By now I have this code (see above) and I want my spinner in one of the tabs of my TabActivity.
There is no obvious error but when I open my activity the tab is empty. I would appreciate some help.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
}
You need to add the Spinner to a layout.
First create a container for the Spinner and then create the Spinner and add it to your container. Next set content of you Activity to your container.
This could be done like this, in your onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
LinearLayout layout = new LinearLayout(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
}
EDIT:
Just to clarify: if the Spinner isn't added to the content of the Activity inside a layout, it isn't visible, so that's why you don't get any errors or anything, because there isn't any errors in your code, per se ;-)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = new LinearLayout(this);
// The following can also be done using a loop
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(MainActivity.this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
}
}

Categories

Resources