I have problem with my spinners,
in my app are two spinners which gets data from database,
when i choose one option from first spinner, i want to remove this option from second spinner,
and add option which i removed from second spinner to first spinner and vice versa.
How can i do that ?
This is my present code:
public void showScoresCalculator() {
setContentView(R.layout.scores_calculator);
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/HelveticaProCn.ttf");
TextView txt = (TextView) findViewById(R.id.txt);
txt.setTypeface(font);
TextView team = (TextView) findViewById(R.id.choose_team);
team.setTypeface(font);
TextView group = (TextView) findViewById(R.id.choose_group);
group.setTypeface(font);
TextView title = (TextView) findViewById(R.id.textView1);
title.setTypeface(font);
TextView title2 = (TextView) findViewById(R.id.textView2);
title2.setTypeface(font);
final Spinner sp = (Spinner) findViewById(R.id.spinner3);
final Spinner sp1 = (Spinner) findViewById(R.id.spinner4);
final DataBaseHelper.MyData[] items = this.dh.getTeamsNameAndIDs("38");
final DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
final DataBaseHelper.MyData[] items2 = this.dh
.getTeamsNameAndIDs("47");
final DataBaseHelper.MyData d1 = items2[(int) sp1.getSelectedItemId()];
final ArrayAdapter<DataBaseHelper.MyData> adapter = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("38"));
final ArrayAdapter<DataBaseHelper.MyData> adapter1 = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("47"));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items2[(int) sp1.getSelectedItemId()];
DataBaseHelper.MyData d1 = items[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d1.getValue(), d.getValue(), d1.getSpinnerText(), d.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
DataBaseHelper.MyData d1 = items2[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d.getValue(), d1.getValue(), d.getSpinnerText(), d1.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
final Button next = (Button) findViewById(R.id.next);
final Button prev = (Button) findViewById(R.id.prev);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showNewsPage();
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showGroupCalculator();
}
});
Page = true;
}
Please, help me ! :)
Update the adapter by removing the selected data from the list. Use Handler concept to update adapter.
Set a new adapter on the Spinner via setAdapter(). Or, if it is an ArrayAdapter, use add(), insert(), and remove() to modify the data. Or, if it is a CursorAdapter, requery() it to get fresh data.
Related
my problem is the following. I have 2 Spinners in one activity. I want that both spinner items should be saved in the DB. But there is a slight difference between both spinners:
Spinner 1: Is populated by data from other db-table - when it is the only spinner in the mentioned activity, it works.
Spinner 2: Has default values. I am not sure if added it correctly to my activity. Anyway, after i added it to my saving the data is not working anymore.
Please help me what am I doing wrong??
My Activity Code:
public class activity_transport extends AppCompatActivity {
private BottomNavigationViewEx bottomNavigationViewEx;
DatabaseHelper myDb;
Button btn_save;
Spinner ChooseProject,ChooseTransport;
EditText Entfernung,Price,MWST;
String selectedspinner, selectedTransport;
ImageView imageView2;
private static int PICK_IMAGE = 100;
Uri imageUri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
myDb = new DatabaseHelper(this);
ChooseProject = (Spinner) findViewById(R.id.ChooseProject);
ChooseTransport = (Spinner) findViewById(R.id.ChooseTransport);
Entfernung = (EditText) findViewById(R.id.Entfernung);
Price = (EditText) findViewById(R.id.Preis);
MWST = (EditText) findViewById(R.id.MwSt);
btn_save=(Button) findViewById(R.id.btn_save);
//bildanzeigebereich im layout
imageView2=(ImageView) findViewById(R.id.imageView2);
loadProjectSpinnerData();
//loadTransportSpinnerData();
SaveData();
//+++++++++++++++Function to load the spinner data from SQLite database++++++++++//
private void loadProjectSpinnerData() {
// database handler
DatabaseHelper db = new DatabaseHelper (getApplicationContext());
// Spinner Drop down elements
List<String> projects = db.getAllProjects();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseProject.setPrompt("Projekt auswählen");
// attaching data adapter to spinner
ChooseProject.setAdapter(dataAdapter);
//Choose Transport Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseProject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.ChooseProject)
{
selectedspinner = (String) ChooseProject.getSelectedItem();
}
else if(spinner.getId() == R.id.ChooseTransport)
{
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/*private void loadTransportSpinnerData() {
// Spinner method to read the on selected value
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Creating adapter for spinner
//ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
//++++++++++++Save Data++++++//
public void SaveData(){
btn_save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.createTransport(
selectedspinner,
selectedTransport,
Integer.valueOf(Price.getText().toString()),
Integer.valueOf(MWST.getText().toString()),
Integer.valueOf(Entfernung.getText().toString()),
imageViewToByte(imageView2)
);
if(isInserted=true)
Toast.makeText(activity_transport.this, "Daten gespeichert", Toast.LENGTH_LONG).show();
else
Toast.makeText(activity_transport.this, "Daten nicht gespeichert", Toast.LENGTH_LONG).show();
}
}
);
}
}
EDIT:
I tried to run the app by setting a few breakpoints.
When trying to run this line selectedTransport = (String) ChooseTransport.getSelectedItem(); the debugger posts Cannot find local variable 'ChooseTransport'
I solved the problem:
I implemented an own method for ChooseTransport- Spinner and now it works. I also figured out that the order how I add the data to the DB was not right. I changed this too.
See here my method for my second spinner:
private void loadTransportSpinnerData(){
ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.transport, android.R.layout.simple_spinner_dropdown_item);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
ChooseTransport.setAdapter(spinnerArrayAdapter);
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
I am creating spinners dynamically when i click add button.but i dont know how to get the values from created dynamic spinners.I need to get values how many shirt selected, saree selected and jeans selected when i click a submit button
this is my code
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
final List<String> dressArray = new ArrayList<String>();
dressArray.add("Select Dress");
dressArray.add("Shirt");
dressArray.add("Saree");
dressArray.add("Jeans");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, dressArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dress = (Spinner) findViewById(R.id.dress);
dress.setAdapter(adapter);
final List<String> dresscountArray = new ArrayList<String>();
dresscountArray.add("Count");
dresscountArray.add("1");
dresscountArray.add("2");
dresscountArray.add("3");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, dresscountArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dresscount = (Spinner) findViewById(R.id.dresscount);
dresscount.setAdapter(adapter1);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Spinner dresscount = (Spinner)addView.findViewById(R.id.dresscount);
Spinner dress = (Spinner)addView.findViewById(R.id.dress);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PickupOrder.this, android.R.layout.simple_spinner_item, dressArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dress.setAdapter(adapter);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
PickupOrder.this, android.R.layout.simple_spinner_item, dresscountArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dresscount.setAdapter(adapter1);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
Firstly define globally
String value1;
int value_position1;
And then call these methods inside onCreate after setting value in each Spinner
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
value_position1 = position;
value1 = parent.getSelectedItem().toString()
.trim();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
And when you call your submit button, use values of each String.
showallPrompt = "";
int Count = new_container.getChildCount();
try {
for (int cc = 0; cc < Count; cc++) {
View child = new_container.getChildAt(cc);
Spinner value = (Spinner)(child.findViewById(R.id.sp_dynamic));
String spin_value = value.getSelectedItem().toString();
showallPrompt += spin_value;
}
} catch (Exception e) {
}
Toast.makeText(MainActivity.this,
showallPrompt,
Toast.LENGTH_LONG).show();
}
}
Do like this on your button click...
my idea is to have a text field where I can input a name manually.
Additionally I do have a list of names. If there is at east 1 name in that list an icon should appear on the right of the EditText in order to open a Spinner to give a selection.
If no item is selected the EditText stays the same and if an item has been selected the EditText will have that item.
I'mworking with visibilities so far - but my problem is that the onNothingSelected seems not to get fired at all.
public class MainActivity extends ActionBarActivity {
private Spinner spinner;
private EditText text2;
private ImageButton button2;
private String spinner_value = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
String[] str = {"","good", "dislike", "like", "hate", "moderate"};
spinner.setPrompt("Set Text");
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, str);
spinner.setAdapter(list);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView tv = (TextView)arg1;
spinner_value = tv.getText().toString();
if(spinner_value.length() == 0)
{
spinner_value = "Nothing";
}
Toast.makeText(getApplicationContext(), spinner_value, Toast.LENGTH_SHORT).show();
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (EditText) findViewById(R.id.editText);
button2 = (ImageButton) findViewById(R.id.imageButton);
text2.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
String myStr = spinner.getSelectedItem().toString();
text2.setText(myStr);
text2.requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplicationContext(), "NothingSelected", Toast.LENGTH_SHORT).show();
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (EditText) findViewById(R.id.editText);
button2 = (ImageButton) findViewById(R.id.imageButton);
text2.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
text2.requestFocus();
}
});
}
}
By default, the item at index 0 is selected when you spinner is created. So, even though the user doesn't actually do anything, the first item is selected. You can see this happen by setting a breakpoint (or some other notification) in onItemSelected() and seeing it get triggered on startup without the user doing anything.
For the onNothingSelected() callback to get triggered, whatever item is selected has to disappear.
I am trying to make app with one text view, one button and one spinner. I make button work showing random values from string array, but i have a lot different string arrays(list1, list2 etc). Now my question is when change spinner position, how to get button get another string array(from spinner) and show it to textview.
How to pass value from spinner when something is selected in spinner to button.
Any help is appreciated.
Here is my code:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Button btn;
public String[] myString,myString1;
public static final Random rgenerator = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//textview
final TextView tv = (TextView) findViewById(R.id.textureView);
final Resources res = getResources();
//string
myString = res.getStringArray(R.array.World_War_I);
myString1 = res.getStringArray(R.array.World_War_II);
//button
btn = (Button) findViewById(R.id.buttonxx);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
if (==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
//drop list
Spinner spinner = (Spinner) findViewById(R.id.spinnerrrr);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.kategorije, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Use this
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
btn.setTag(pos+""); // Passing as string
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
and then
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
int index = Integer.parseInt(btn.getTag().toString());
if (index==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (index==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
I have a spinner where the user enters items dynamically.
I want when the user selects an item from spinner to start another activity.
I use:
...
m_myDynamicSpinner = (Spinner)findViewById(R.id.myspinner);
m_addItemText = (EditText)findViewById(R.id.editTextItem);
Button addButton = (Button)findViewById(R.id.add);
Button removeButton = (Button)findViewById(R.id.remove);
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("Items List");
....
}
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
}
private void clearSpinnerItems() {
m_adapterForSpinner.clear();
m_adapterForSpinner.add("Items List");
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
m_addItemText.setText((CharSequence)parent.getItemAtPosition(pos).toString());
if (pos == 1){
Intent intent = new Intent(MainActivity.this, selection.class);
MainActivity.this.startActivity(intent);
}
}
but nothing happens.
Add this to you code
m_myDynamicSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Hope this helped you.