Reputation: 192
I am using this follwing code .
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name=nameEdit.getText().toString();
String contactnumber=numberEdit.getText().toString();
String quantity = String.valueOf(quantitySpinner.getSelectedItem());
String noofpieces=piecesEdit.getText().toString();
String dateandtime=editDateTime.getText().toString();
String deliveryaddress=deliveryEdit.getText().toString();
long val=adapter.insertDetails(name, contactnumber, quantity, noofpieces, dateandtime,OPTIONS ,deliveryaddress);
String sms="Name:"+name+ System.getProperty ("line.separator")+"ContactNumber:"+contactnumber+ System.getProperty ("line.separator")+"Quantity:"+quantity+System.getProperty ("line.separator")+"Number.of.Pcs:"+noofpieces+System.getProperty ("line.separator")+"Date and Time:"+dateandtime+System.getProperty ("line.separator")+"Delivary Address:"+deliveryaddress;
int bookingId = adapter.getMaxID();
Bundle passdata = new Bundle();
passdata.putInt(helper_ob.B_ID, bookingId);
passdata.putString(helper_ob.QUANTITY, quantity);
passdata.putString(helper_ob.SMS, sms);
Intent passIntent = new Intent(BookingForm.this, OrderSummary.class);
passIntent.putExtras(passdata);
startActivity(passIntent);finish();
}
});
and have inserted into database table .while updating the Booking form .I can get all things to edit except the spinner .The Spinner value is set as default.
spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android</string>
<string name="action_settings">Settings</string>
<string name="bottle_prompt">Select your Quantity</string>
<string-array name="bottle_arrays">
<item >300ml</item>
<item>500ml</item>
<item>1-litre</item>
<item>2-litre</item>
<item>5-litre</item>
<item>20-litre</item>
<item>50-litre</item>
<item>New Bt</item>
<item>New Matca</item>
</string-array>
</resources>
For eg: If i am selecting the 1-liter in spinner and inserting into database table while updating all the values are opened in the edit page except the spinner is set to default 300ml.How can i update the spinner while going to edit page?
Edit.java
package com.example.android;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class EditBookingForm extends Activity {
EditText nameEdit,contactNumber,editPieces,editDate,editAddress;
Spinner spinner;
RadioGroup radioGroup;
RadioButton radioButton;
Cursor cursor;
BookingAdapter adapter;
BookingOpenHelper openHelper_ob;
Button doneButton,cancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_online);
nameEdit=(EditText) findViewById(R.id.nameEdit);
contactNumber=(EditText) findViewById(R.id.contactNumber);
editPieces=(EditText) findViewById(R.id.editpieces);
editDate=(EditText) findViewById(R.id.editdate);
editAddress=(EditText) findViewById(R.id.editdelivary);
spinner=(Spinner) findViewById(R.id.spinner);
radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
doneButton=(Button) findViewById(R.id.updateformbtn);
cancelButton=(Button) findViewById(R.id.deleteformbutton);
Bundle extras = getIntent().getExtras();
final int bookingId = extras.getInt(openHelper_ob.B_ID);
adapter = new BookingAdapter(this);
cursor = adapter.queryAll(bookingId);
if (cursor.moveToFirst()) {
do {
nameEdit.setText(cursor.getString(1));
contactNumber.setText(cursor.getString(2));
//spinner.setSelection(q);
editPieces.setText(cursor.getString(4));
editDate.setText(cursor.getString(5));
editAddress.setText(cursor.getString(6));
} while (cursor.moveToNext());
}
doneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String name = nameEdit.getText().toString();
String number = contactNumber.getText().toString();
String quantity = String.valueOf(spinner.getSelectedItem());
String noofpieces=editPieces.getText().toString();
String dateandtime=editDate.getText().toString();
String deliveryaddress=editAddress.getText().toString();
adapter.updateldetail(bookingId,name,number);
finish();
Bundle passdata = new Bundle();
String sms="Name:"+name+ System.getProperty ("line.separator")+"ContactNumber:"+number+ System.getProperty ("line.separator")+"Quantity:"+quantity+System.getProperty ("line.separator")+"Number.of.Pcs:"+noofpieces+System.getProperty ("line.separator")+"Date and Time:"+dateandtime+System.getProperty ("line.separator")+"Delivary Address:"+deliveryaddress;
passdata.putInt(openHelper_ob.B_ID, bookingId);
passdata.putString(openHelper_ob.SMS, sms);
Intent passIntent = new Intent(EditBookingForm.this, OrderSummary.class);
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
}
}
Upvotes: 0
Views: 1395
Reputation: 192
Spinner spinner;
//storing the textvalue in quantity
String quantity = String.valueOf(spinner.getSelectedItem());
//storing the getSelectedItemPosition in int variable
int qualityValue=spinner.getSelectedItemPosition();
Upvotes: 0
Reputation: 6096
The way to get the selection for the spinner is:
int spinnerValue = spinner.getSelectedItemPosition();
Documentation reference: http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
Upvotes: 1
Reputation: 1851
spinner.setSelection(POSITION);
this will set the selection to the desired position where position can be found from the array elements in string file
Upvotes: 0