我試圖讓我的setOnItemClickListener正常工作,但是當我添加該方法時,Eclipse說我應該刪除「@Override」註釋,如果我這樣做,那麼偵聽器不起作用。事情是,我需要捕捉的「click」事件,做一些事情...... 這裏是我的方法:setOnItemClickListener無法正常工作
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;
}
}
如果我刪除註釋,我的方法不捕獲事件。如果我不刪除它,我的應用程序不會編譯。 BTW正在使用Android 2.2 SDK
這是在你的'onCreate()'裏面嗎? – codeMagic 2013-04-11 01:14:00
@codeMagic它看起來像。 – Cristian 2013-04-11 01:14:41
是的,它是在onCreate() – 2013-04-11 01:16:47