我试图让我的 setOnItemClickListener 工作,但是当我添加方法时,Eclipse 说我应该删除“@Override”注释,如果我这样做了,监听器就不起作用。问题是我需要捕获“点击”事件并做一些事情......这是我的方法:
list.setOnItemClickListener(new OnItemClickListener() {
@Override //<-- this is the annotation that eclipse says I should remove
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
我真的很确定问题不在于方法本身,我想它是别的东西。请查看我的活动并告诉我做错了什么:
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class Sounds extends Activity{
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_table);
list=(ListView)findViewById(R.id.list);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
DB_Helper myDbHelper = new DB_Helper(this);
try {
myDbHelper.createDataBase();
}catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
String[] columns = new String[3];
columns[0] = "id_Category";
columns[1] = "category_name";
columns[2] = "image_name";
songsList = myDbHelper.selectAll("Categories",columns);
}catch(SQLException sqle){
throw sqle;
}
adapter = new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//ToDo something...
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.about_sounds, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.about_sounds_item1:
Intent talesToLearnActivity = new Intent(Sounds.this, AboutSounds.class);
startActivity(talesToLearnActivity);
break;
default:
break;
}
return true;
}
}
如果我删除注释,我的方法不会捕获事件。如果我不删除它,我的应用程序不会编译。顺便说一句,我正在使用 Android 2.2 SDK