void 是变量 onListItemClick 的无效类型?
我一直在尝试在扩展ListActivity
的android活动上设置一个简单的onClickListener
。
在此类的 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);
}
}
}
I've been trying to set up a simple onClickListener
on an android activity which extends ListActivity
.
Within this classes on create method, I create a spinner object which when a user selects an item, an arrayAdapter
list item is created, depending on what the user selected (populated from a cursor request). This is generated within another method within the same class.
ArrayAdapter<String> playerAdapter = new ArrayAdapter<String>(this,
R.layout.match_update_rows, R.id.player_name, playerItems);
setListAdapter(playerAdapter);
I'm trying to add a click listener on this new list. I have added:
@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);
}
However, I'm getting an error on the protected void onListItemClick
line.
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
Also, if I select the onListItemClick
line, Eclipse says 'void is an invalid type for the variable onListItemClick'
?
All works if I remove the click listener code, just cannot work out how to add the listener on the arrayAdapter
item so I can tell which item the user has clicked on -- any help appreciated!
As requested by MyD - here is the complete class listing:
Thanks for your prompt responses. In reply to MByD here is the complete code listing for this class. Any suggestions appreciated!
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
方法声明需要在类定义的上下文中进行。当尝试在另一个方法中声明一个方法时,会报告类似的错误。
Method declarations need to be made in the context of a class definition. When trying to declare a method inside another method, errors similar to this are reported.
我刚刚在尝试将
OnClick
处理程序放入MenuItem
方法中时遇到此错误,因此请检查您是否在MenuItem
之外。I just got this error from trying to put an
OnClick
handler within aMenuItem
method, so check you are outside of aMenuItem
.