views:

43

answers:

1

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;
import 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.

+1  A: 

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);
}
Pentium10
How would i move the cursor to that position?
User358218
See my edit to the question
Pentium10
Sorry to clarify, spinnerCursor would be my Cursor and how would i read the values from the cursor? My method would be db.getAllData()
User358218
you can reference each column by a number, or by it's name. Cursor has several methods with get...() those will help you. Search the site, there are plenty of questions/samples to work with cursor http://stackoverflow.com/search?q=android+cursor
Pentium10
Ok sure thank you.
User358218