从spinner中的选项查询SQLite数据到TextView

时间:2012-09-03 21:22:21

标签: android sqlite spinner

我正在查询我的数据库以填充我的微调器选择(这很好)但我还想从所选位置填充其余Activity中的一些TextView。 这是我的类来处理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

}

}

以下是我尝试在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;
}

1 个答案:

答案 0 :(得分:2)

嗯,我不确定问题是什么......但这是我的猜测:

我在onCreate()中看不到这一行:

spinner.setOnItemSelectedListener(this);

另请阅读Java naming convention,其中建议SportText等变量应以sportText等小写字母开头。


我注意到你同时拥有dbInfo,但他们做同样的事情。您可以删除其中一个。