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());
Related
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);
}
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);
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.
i trying tu put some elements from database to a List view,
my problem is that when i starrt my activity, I get this:
**com.example.restaurant.Restaurant#2be2d1d0
com.example.restaurant.Restaurant#2be3d3a8**
instead of database objects.
public class Liste extends Activity{
private ListView listview;
private Button boutonAjouter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_restaurant);
listview = (ListView)findViewById(R.id.listView1);
Restaurant mcdo = new Restaurant("mcdo","brossard","take-out","fastfood","450-555-5555");
Restaurant burger = new Restaurant("burger","longueuil","take-out","fastfood","450-999-9999");
RestaurantBDD restaurantBDD = new RestaurantBDD(this);
restaurantBDD.openForWrite();
restaurantBDD.insertRestaurant(mcdo);
restaurantBDD.insertRestaurant(burger);
ArrayList <Restaurant> restaurantlist = restaurantBDD.getAllRestaurants();
restaurantBDD.close();
ArrayAdapter <Restaurant> adapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, restaurantlist);
listview.setAdapter(adapter);
boutonAjouter = (Button) findViewById(R.id.btn_nouveau);
boutonAjouter.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Liste.this, Formulaire.class);
startActivity(intent);
}
});
}
}
If you want to show a list with the names of the restaurants, supposing that your Restaurant class contains a string attribute "name", you should first create an array containing the names of the restaurants and then use an ArrayAdapter <String>:
String[] values = new String[restaurantlist.size()];
for(int i=0;i<restaurantlist.size();i++) {
values[i] = restaurantlist.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);
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);
}
}