我正在查询我的数据库以填充我的微调器选择(这工作正常),但我还想从所选位置在 Activity 的其余部分填充一些 TextView。这是我处理活动的类:
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;
}