0

我正在查询我的数据库以填充我的微调器选择(这工作正常),但我还想从所选位置在 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;
}
4

1 回答 1

2

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

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

spinner.setOnItemSelectedListener(this);

另请阅读Java 命名约定,它建议变量 likeSportText应以小写字母 like 开头sportText


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

于 2012-09-03T21:30:37.990 回答