spinner android set selection alternative - android

I created 2 Spinners and I filled them with an array of numbers. What I want is that the user chooses the number they want on the first one, and then I save it to a variable that I use to populate the dropdown list of the second one with values that start after that number. How can I do this? How can I change the text in the second spinner to start with the number chosen.
I have this, I store the item of the Spinner on the variable x, and I want a second Spinner to start with that variable.
mySpinner.setAdapter(new ArrayAdapter<String>(NewGuiaActivity.this, android.R.layout.simple_spinner_dropdown_item,
establist));
// Spinner on item click listener
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// Locate the textviews in activity_main.xml
arg0.setSelection(5);
x = estab.get(position).getID();
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
the arg0.setSelection(5), does the trick but it dont let me choose another value when i click on a diferent item of the spinner how can i change this.

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.

List item having button

I am working on Android application.
I have a ListView. One of the item has a button, and others have simple text views. I have now requirement of showing button as pressed when it is pressed. I am able to do this.But when I connect the device via. Bluetooth keyboard and navigate through the list. I see list items being selected as expected.But I want the item with the button to look as if button has been selected (focused).
Thanks
Krishna
use
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

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

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
}});

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);
}

After item clicked in spinner, Display dialog

Is it possible to have a spinner that displays a dialog when a item is pressed in the list
and is it good code and not too messy. If so could someone show me some code so i can see the make up of this done thank you
You can add an item selected listener on your spinner and then while an item is selected or touched start a dialog. like this --
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Here add the Dialog you want !", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Categories

Resources