gpt4 book ai didi

android - setOnItemClickListener 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:54 25 4
gpt4 key购买 nike

我试图让我的 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) {


}
});

我真的确定问题不是方法本身,我想是其他原因。请查看我的 Activity 并告诉我哪里做错了:

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

最佳答案

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

关于android - setOnItemClickListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com