setOnItemClickListener无法正常工作

时间:2013-04-11 01:04:26

标签: android listview onclicklistener

我试图让我的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;
 }
}

如果删除注释,我的方法不会捕获该事件。如果我不删除它,我的应用程序不会编译。 BTW正在使用Android 2.2 SDK

2 个答案:

答案 0 :(得分:3)

检查项目属性并确保Java Compiler -> JDK Compliance -> Complier Compliance Level为1.6。

答案 1 :(得分:2)

检查项目中的属性并确保Java Compiler - &gt; JDK合规性 - &gt; Complier Compliance Level是1.6。否则它将不起作用。