Update the Activity based on what item is selected in the Spinner - android

In the current Activity I'm printing a graph (using Androidplot) of the closing prices of a selected stock from the previous activity.
In this activity I have a spinner of a list of indicators the user can overlay.
Now I want the graph to be redrawn with this new selection from the spinner.
I did try refreshing/reloading the page onItemSelected but that causes the page to keep refreshing even without waiting for a user input.
public class DispGraph extends Activity {
private XYPlot plotstock;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.graph);
// PRINTING SELECTED STOCK_NAME
Bundle bundle = getIntent().getExtras();
String sname = bundle.getString("SN");
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(sname);
// INDICATOR LIST
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Indicators, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
String iname = spinner.getSelectedItem().toString();
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
startActivity(getIntent());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
//PASSING STOCK-TICKER & INDICATOR TO PHP
// PLOTTING GRAPH
plotstock = (XYPlot) findViewById(R.id.mySimpleXYPlot);
Number[] series1Numbers = ind;
Number[] series2Numbers = closing;
XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, iname);
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Closing Price");
plotstock.setDomainLabel("Date");
plotstock.setRangeLabel("Price");
plotstock.addSeries(series1,new LineAndPointFormatter(Color.rgb(0, 200, 0), Color.rgb(0,100, 0), null, new PointLabelFormatter(Color.TRANSPARENT)));
plotstock.addSeries(series2,new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100),null, new PointLabelFormatter(Color.TRANSPARENT)));
plotstock.setTicksPerRangeLabel(2);
}
}

Using your current approach, each time the user select a spinner value your current activity will be started again, so it will be put on Android Stack. Soon, if the user change your spinner n times, you should press n + 1 times to close your app. This is a really annoying behavior.
Well, I do not know the AndroidPlot, but looking its API I found the method redraw.So, i think you could try on that way:
Every time the user select a spinner value, i can do your modifications on plotstock after call plotstock.redraw()
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//Change the graphics value here!
plotstock.redraw();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});

Related

Android filter listview like Instagram

I'm creating an Android app in which there is a listeview and the search is filtered through an EditText.
What I want to know is how does Instagram (for example) show the preview of searches divided into categories?
Let me explain better, if on Instagram I look for example "London" I will come out in a list of fields where for example there is the city icon and with the word "London" next to it, underneath there is a field with "#london" written on it and another field with "London" and the user's photo below.
What are the names of the searches divided into categories or sections in "Programming"?
I would like to do it in my App but the problem is that I don't know how to call this thing.
If I have a listview and want to filter it through a search, while I type the word "London" it immediately shows me the Items with the same name as "London", instead I want a search like that of Instagram where the results are filtered first categories and then when you click on the category it shows you the results.
What is the name of this practice?
Use something like:
private ArrayAdapter<String> mAdapter;
private String[] data = {"myname","myname2","myname3","myname4","myname5"};
private EditText searchBox;
private ListView mListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
searchBox = findViewById(R.id.searchBox);
mListView = findViewById(R.id.mListView);
mListView.setTextFilterEnabled(true);
mAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, data);
mListView.setAdapter(mAdapter);
searchBox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
MainActivity.this.mAdapter.getFilter().filter(arg0);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
Now if you enter "myname2" in the edittext, ListView will only show it. You can use the similar approach in case of GridView/RecyclerView.

Save spinner and use it for another activity

I have tried to use the examples and read those links that I think related to my project, but all of it have a different structure from mine. Hope anybody could help.
I have 3 spinners in my NewProfile.java class. In this class, user can input their profiles and also selecting the type of workout with the user interests. I have no problem saving the profile text input. But I have a problem to save the spinner value selected by the user. Following are my codes.
NewProfile.java
public class NewProfile extends Activity {
EditText profileName, profileDOB, profileSex, profileWeight, profileHeight;
Spinner workoutspinner, levelspinner, weightplan1, weightplan2,
weightplan3;
TextView display;
DBController controller = new DBController(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newdata);
profileName = (EditText) findViewById(R.id.etName);
profileDOB = (EditText) findViewById(R.id.etDOB);
profileSex = (EditText) findViewById(R.id.etSex);
profileWeight = (EditText) findViewById(R.id.etWeight);
profileHeight = (EditText) findViewById(R.id.etHeight);
chooseWorkout();
chooseLevel();
chooseWeight1();
chooseWeight2();
chooseWeight3();
}
public void addNewProfile(View view) {
HashMap<String, String> queryValuesMap = new HashMap<String, String>();
queryValuesMap.put("profileName", profileName.getText().toString());
queryValuesMap.put("profileDOB", profileDOB.getText().toString());
queryValuesMap.put("profileSex", profileSex.getText().toString());
queryValuesMap.put("profileWeight", profileWeight.getText().toString());
queryValuesMap.put("profileHeight", profileHeight.getText().toString());
controller.insertProfile(queryValuesMap);
this.startingpointActivity(view);
}
private void startingpointActivity(View view) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getApplicationContext(),
StartingPoint.class);
startActivity(objIntent);
finish();
}
private void chooseWorkout() {
// TODO Auto-generated method stub
workoutspinner = (Spinner) findViewById(R.id.spinWorkout);
ArrayAdapter<CharSequence> workoutadapter = ArrayAdapter
.createFromResource(this, R.array.saWorkout,
android.R.layout.simple_spinner_item);
workoutadapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
workoutspinner.setAdapter(workoutadapter);
workoutspinner.setOnItemSelectedListener(new workoutOnClickListener());
}
private void chooseLevel() {
// TODO Auto-generated method stub
levelspinner = (Spinner) findViewById(R.id.spinLevel);
ArrayAdapter<CharSequence> leveladapter = ArrayAdapter
.createFromResource(this, R.array.saLevel,
android.R.layout.simple_spinner_item);
leveladapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
levelspinner.setAdapter(leveladapter);
levelspinner.setOnItemSelectedListener(new levelOnClickListener());
}
private void chooseWeight1() {
// TODO Auto-generated method stub
weightplan1 = (Spinner) findViewById(R.id.spinWeight);
ArrayAdapter<CharSequence> weightadapter1 = ArrayAdapter
.createFromResource(this, R.array.saWeight1,
android.R.layout.simple_spinner_item);
weightadapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightplan1.setAdapter(weightadapter1);
}
private void chooseWeight2() {
// TODO Auto-generated method stub
weightplan2 = (Spinner) findViewById(R.id.spinWeight);
ArrayAdapter<CharSequence> weightadapter2 = ArrayAdapter
.createFromResource(this, R.array.saWeight2,
android.R.layout.simple_spinner_item);
weightadapter2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightplan2.setAdapter(weightadapter2);
}
private void chooseWeight3() {
// TODO Auto-generated method stub
weightplan3 = (Spinner) findViewById(R.id.spinWeight);
ArrayAdapter<CharSequence> weightadapter3 = ArrayAdapter
.createFromResource(this, R.array.saWeight3,
android.R.layout.simple_spinner_item);
weightadapter3
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightplan3.setAdapter(weightadapter3);
}
public class workoutOnClickListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
parent.getItemAtPosition(pos);
if (pos == 0) {
chooseLevel();
levelspinner.setClickable(true);
weightplan1.setClickable(true);
weightplan2.setClickable(true);
weightplan3.setClickable(true);
} else if (pos != 0){
levelspinner.setClickable(false);
weightplan1.setClickable(false);
weightplan2.setClickable(false);
weightplan3.setClickable(false);
Toast.makeText(getApplicationContext(),
"Wollaaaa! Only Program 1 is available for this moment. Sorry.. :)",
Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
public class levelOnClickListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
// TODO Auto-generated method stub
parent.getItemAtPosition(pos);
if (pos == 0) {
chooseWeight1();
} else if (pos == 1){
chooseWeight2();
} else if (pos == 2){
chooseWeight3();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
Do I need to change anything in my DBController in order to save these spinners? Please help. Thanks.
p/s Just to inform that the addNewProfile is a button that save all the information.
Okay, I'll do my best to answer as requested:
For a start, these are the key parts of setting and getting values from your spinners:
If you need to set the values in your spinners, you can use this.
mySpinner.setSelection(arrayAdapter.getPosition("Category 2")); //This assumes that "Category 2" exists in your spinner list.
To get the value from your spinner.
String Text = mySpinner.getSelectedItem().toString();
Here is a great discussion on what to DO with those values.
SharedPreferences example.
That pretty well explains how to save your preferences. It also should give you almost everything to answer your initial question.
The key part afterward is what to do with those prefs. This line shows how to set a string based on what it finds in the saved prefs, and if it finds nothing, will load "default".
This is helpful for when the user is running the app for the first time and prefs have never been set.
test_string=shared_preferences.getString("test_key", "Default");
Once those prefs have been set, and you want them to show in a different view, just add all this same sort of technique to the other view.
Hope that helps. For what it's worth, every last bit of this was taken from here, through various searches. Sometimes you need to look through a bunch of different things to find an answer if a simple solution is not in one place.

Dialog Spinner does not return value

I have a dialog setup that has two spinners connected to a cursor. I have worked through a couple of problems with the help of this site, but I can not seem to get past this point. Everything I find are things I have already tried.
The problem is that when I click on a spinner selection or click Submit to exit the dialog, the spinner value is not the value it should be. I am getting the package name with some code. I am trying to get the string from the spinner with .getSelectedItem().toString();
I currently have the code set up to use onItemSelected, but before that I tried to use the getItemSelected once Submit was clicked. Neither seem to work.
Here is the code for this section.
At the end the values are going into a textview. The value shown is "android.database.sqlite.sqliteCursor#414175e0"
Any ideas?
private void transfer() {
dialog = new Dialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth);
dialog.setContentView(R.layout.transfer_dialog);
dialog.setTitle(R.string.transfer_accounts);
Button btnCancel = (Button)dialog.findViewById(R.id.btnCancel);
Button btnSubmit = (Button)dialog.findViewById(R.id.btnSubmit);
Cursor load_spinner = mDbHelper.spinnerAccounts();
startManagingCursor(load_spinner);
String[] columns = new String[] { RegisterDbAdapter.ACCOUNTS_ACCOUNT };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, load_spinner, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFrom = (Spinner)dialog.findViewById(R.id.spinnerFrom);
spinnerTo = (Spinner)dialog.findViewById(R.id.spinnerTo);
spinnerFrom.setAdapter(mAdapter);
spinnerTo.setAdapter(mAdapter);
dialog.show();
spinnerFrom.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
fromAccount = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinnerTo.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
toAccount = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tvFrom.setText(fromAccount);
tvTo.setText(toAccount);
dialog.dismiss();
}
});
}
You need two separate adapters for two spinners.
You are only using one... each time you make a selection, the adapter sets your selection to your choice ( for both of the spinners since you have the same adapter backing each one ).
Create a second adapter and things should work more like you expect.
Also, try this in your onItemSelected method (using the parameter names you show):
String fromAccount = parent.getItemAtPosition(arg2).toString();
I think you have to use your cursor to get the String you want. Arg2 should be the selected position. Use it to place your cursor to the right data row and use the getstring method of the cursor to get the string from the database column.
So I finally got it to work. Thanks for the suggestions, I really do appreciate them, but they didn't work for me. I am not sure why the code will not return the value, since the way I tried it supposedly works for others.
Anyway, I found a way to get my values by using 'Long arg3'. Since it holds the rowId of the value in the table, I used that to return the selection in the spinner. I am posting the code for this below.
Thanks again.
The code from the selected spinner:
spinnerFrom.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
getAccountName(arg3);
fromAccount = returnAccount;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
And here is the method I wrote to return the value I wanted. I put it a separate method because it is getting used from multiple spinners. The code:
private void getAccountName(long arg3) {
if (returnAccount != null){
returnAccount = null;
}
RegisterDbAdapter tAdaptor = new RegisterDbAdapter(this);
tAdaptor.open();
Cursor tCursor = tAdaptor.fetchAccount(arg3);
startManagingCursor(tCursor);
returnAccount = tCursor.getString(1);
}

How to handle an onClickListener for a Button within the Spinner onItemSelected(...) method?

I have a Spinner.onItemSelected() method and I want to be able to have an event happen once a Button is clicked after selecting the Spinner item.
For example if you select Beginner for your Spinner and click Java for another spinner. Under that I have a button that says Start. How do I have a Button.onClick event correspond with the selected spinner options?
I did something like this but what I assigned as the setOnClickListener() value is not being read by the View.OnClickListener.
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selected = (Integer) arg0.getItemAtPosition(0);
position = spinner.getSelectedItemPosition();
start = (Button)findViewById(R.id.start);
start.setOnClickListener(phaseHandler);
View.OnClickListener phaseHandler = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
}
The phaseHandler I declared is not being read by start.setOnClickListener(phaseHandler) so this results in my View.OnCLickListenr invocation not working because phaseHandler is not being set to the Button start. In Eclipse my phaseHandler has the red underlining curly for an error where it says start.setOnClickListener(phaseHandler);
Any Ideas?
move start.setOnClickListener(phaseHandler); down
this may help you
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selected = (Integer) arg0.getItemAtPosition(0);
position = spinner.getSelectedItemPosition();
start = (Button)findViewById(R.id.start);
View.OnClickListener phaseHandler = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
start.setOnClickListener(phaseHandler);
}

Is there a way for a spinner option to open another spinner?

I want it where when someone clicks an option in a Spinner, it opens another spinner with more options. Also, is there a way for an "Other" option to open an EditText where someone can input their selection if theirs isn't available in the Spinner?
Example:
Spinner 1 has these options:
iOS
Android
And if they select iOS, another spinner comes up immediately where the options are all the iPhone versions. (i.e., titled "Which iPhone do you have?")
And if they select Android, it does the same thing, but with Android devices.
AND if their phone isn't on the second spinner, they type the model of their phone in.
How could I do this if I have the first spinner already in my code?
P.S., if needed, I can post the code for the first spinner, though it's pretty standard.
Basically build your second spinner programmatically depending on which option they choose. I'd add "other" to each second spinner. If they choose "other" then you can display the text box.
I hope this will be useful to you.
Try this Code...
public class MainActivity extends Activity {
Spinner sp1,sp2;
ArrayAdapter<String> adp1,adp2;
List<String> l1,l2;
int pos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=new ArrayList<String>();
l1.add("A");
l1.add("B");
sp1= (Spinner) findViewById(R.id.spinner1);
sp2= (Spinner) findViewById(R.id.spinner2);
adp1=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,l1);
adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(adp1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
pos=arg2;
add();
}
private void add() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();
switch(pos)
{
case 0:
l2= new ArrayList<String>();
l2.add("A 1");
l2.add("A 2");
adp2=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line,l2);
adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(adp2);
select();
break;
case 1:
l2= new ArrayList<String>();
l2.add("B 1");
l2.add("B 2");
adp2=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line,l2);
adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(adp2);
select();
break;
}
}
private void select() {
// TODO Auto-generated method stub
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Test "+arg2, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}

Categories

Resources