我一直在尝试在扩展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);
}
}
}
答案 0 :(得分:5)
方法声明需要在类定义的上下文中进行。当尝试在另一个方法中声明一个方法时,会报告与此类似的错误。
答案 1 :(得分:0)
我尝试在OnClick
方法中设置MenuItem
处理程序时遇到此错误,因此请检查您是否在MenuItem
之外。