Question with android
How would i make what i selected in the spinner appear in a textview when i choose that selection? The data would be taken from the database.
My Booking.java
package one.two;
import android.app.Activity;
impor开发者_如何学JAVAt android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Booking extends Activity
{
private DBAdapter db;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
db.open();
setContentView(R.layout.booking);
Cursor spinnerCursor = db.getAllData();
startManagingCursor(spinnerCursor);
Spinner colourSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.array_arrival, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
colourSpinner.setAdapter(adapter);
colourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
}
Thank you.
What you have to do is
- make your cursor a member of the
Booking
class - make your textview a member of the
Booking
class - based on the
position
param move the cursor to that position. - read the values from the cursor
- then put them in TextViews
onItemSelected:
if (Booking.this.spinnerCursor.moveToPosition(position)) {
String title=Booking.this.spinnerCursor.getString(1);
Booking.this.myTextView.setText(title);
}
精彩评论