Brian White
Brian White

Reputation: 307

Querying SQLite Data to a TextView from a choice in a spinner

I am querying my db to fill my spinner selections (this works fine) but I also want to populate some TextViews in the rest of the Activity from the selected position. This is my class to handle the Activity:

public class Post_Player_Info extends Activity implements OnItemSelectedListener{

DBAdapter db = new DBAdapter(this);
Spinner spinner;
TextView SportType;
Button Left, Right, Front, Back;
DBAdapter Info = new DBAdapter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.postplayerinfo);

    spinner = (Spinner) findViewById(R.id.spinner1);
    SportType = (TextView) findViewById(R.id.tvSportType);

    db.open();

    Cursor c = db.getNames();

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,
        c,  new String[] {DBAdapter.ROW_FIRSTNAME}, 
        new int[] {android.R.id.text1});

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    db.close();

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

    int position = spinner.getSelectedItemPosition();
    switch (position) {
    case 0: 
        Info.open();
        String data = Info.getData();
        SportType.setText(data);
        Info.close();
    break;

    }

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

Here is where I am trying to get the data in my dbhelper:

public String getData() {
    // TODO Auto-generated method stub
    String [] columns = new String [] {ROW_SPORT};
    Cursor c = db.query(DATABASE_TABLE1, columns, null, null, null, null, null);
    String results = "";

    int sport = c.getColumnIndex(ROW_SPORT);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        results = results + c.getString(sport) + " ";
    }
    return results;
}

Upvotes: 0

Views: 670

Answers (1)

Sam
Sam

Reputation: 86948

Hmm, I'm not sure what the problem is... But this is my guess:

I don't see this line in onCreate():

spinner.setOnItemSelectedListener(this);

Also please read about Java naming convention, which suggests that variables like SportText should start with a lowercase letter like sportText.


I noticed that you have both db and Info, but they do the same thing. You can remove one of these.

Upvotes: 2

Related Questions