我一直在尝试在扩展onClickListener
的android活动上设置简单的ListActivity
。
在create方法的此类中,我创建了一个微调器对象,当用户选择一个项目时,将根据用户选择的内容(从光标请求中填充)创建一个arrayAdapter
列表项。这是在同一类的另一个方法中生成的。
ArrayAdapter<String> playerAdapter = new ArrayAdapter<String>(this,
R.layout.match_update_rows, R.id.player_name, playerItems);
setListAdapter(playerAdapter);
我正在尝试在此新列表上添加点击监听器。我已经添加了:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, PlayerFixtureScore.class);
i.putExtra("RowId", id);
startActivity(i);
}
但是,我在
protected void onListItemClick
行上遇到错误。Multiple markers at this line
- Syntax error on token ")", ; expected
- Syntax error on token ",", ; expected
- Duplicate local variable position
- Syntax error on token ",", ; expected
- Duplicate local variable id
- Duplicate local variable l
- Syntax error on token "(", ; expected
- Duplicate local variable v
- Syntax error on token ",", ; expected
- void is an invalid type for the variable
onListItemClick
另外,如果我选择
onListItemClick
行,Eclipse会说'void is an invalid type for the variable onListItemClick'
?如果删除了单击侦听器代码,所有方法都将起作用,只是无法确定如何在
arrayAdapter
项目上添加侦听器,这样我就可以知道用户单击了哪个项目-感谢您提供任何帮助!根据MyD的要求-这是完整的类清单:
感谢您的及时答复。作为对MByD的答复,这里是此类的完整代码清单。任何建议表示赞赏!
public class MatchUpdate extends ListActivity {
/** Called when the activity is first created. */
private int cursorCounter = 0;
private int spinnerSelected;
/** Set up the db for inital use */
DataBaseHelper myDbHelper = new DataBaseHelper(this);
Match match;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.match_update);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
// create a spinner for fixture selection
Spinner fixtureSpin = (Spinner) findViewById(R.id.fixtures);
// Get all fixtures for Spinner...
Cursor cur = myDbHelper.getFixtures();
// create match object for cursor results
match = new Match(cur.getCount());
// Populate match object with Cursor results
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
match.setFixtureId(cursorCounter, cur.getInt(0));
match.setHomeId(cursorCounter, cur.getInt(1));
match.setHomeTeam(cursorCounter, cur.getString(2));
match.setAwayId(cursorCounter, cur.getInt(3));
match.setAwayTeam(cursorCounter, cur.getString(4));
match.setDate(cursorCounter, cur.getString(5));
cursorCounter++;
}
// Set up adapters for fixture spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, match.getFixtureList());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Set up fixture spinner
fixtureSpin.setAdapter(adapter);
// Set fixture spinner listener
fixtureSpin.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
spinnerSelected = parent.getSelectedItemPosition();
updatePlayers(match);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
public void updatePlayers(Match thisMatch) {
// Get players for selected fixture based on Spinner selection
Cursor playerCur = myDbHelper.getFixturePlayers(
thisMatch.getHomeId(spinnerSelected),
thisMatch.getAwayId(spinnerSelected));
// set up simple ArrayAdapter to hold player names
String[] playerItems;
playerItems = new String[playerCur.getCount()];
int cnt = 0;
// pull player data from cursor, into a simple array
for (playerCur.moveToFirst(); !playerCur.isAfterLast(); playerCur
.moveToNext()) {
playerItems[cnt] = playerCur.getString(0);
cnt++;
}
ArrayAdapter<String> playerAdapter = new ArrayAdapter<String>(this,
R.layout.match_update_rows, R.id.player_name, playerItems);
setListAdapter(playerAdapter);
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, PlayerFixtureScore.class);
i.putExtra("RowId", id);
startActivity(i);
}
}
}
最佳答案
方法声明需要在类定义的上下文中进行。当尝试在另一个方法中声明一个方法时,会报告与此类似的错误。