我的 OnItemClickListener 有问题。请注意,我是按照 android 开发者网站提供的指南编写的:http: //developer.android.com/resources/tutorials/views/hello-listview.html
设置监听器时:
lv.setOnItemClickListener(new OnItemClickListener() {
我为新的“OnItemClickListener”显示了以下错误
“类型 new AdapterView.OnItemClickListener(){} 必须实现继承的抽象方法 AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)”
我尝试了 eclipse 的建议并添加了明显未实现的方法,但它只是生成了一个除了参数中的实际细节之外几乎相同的方法,例如我有
"int position"
自动生成的代码有
"int arg2"
并且通过使用这种方法,它使我的方法变得多余,因为它不会被调用(我认为......)
我试图更改代码的各个部分,例如参数的内容等,但我没有运气。所以我基本上是在寻找一些帮助来解决这个问题,任何建议/方向将不胜感激。我的完整代码如下所示:
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class View extends ListActivity{
static final String[] entriesArray = new String[]{
"One", "Two", "Three", "GO"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.dbrowviewer, entriesArray));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//When clicked show toast
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
和布局以防万一.. (R.layout.dbrowviewer)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
Also if anyone has a spare second to give advice..I have an error with "(TextView) view.getText()" in the toast which states that I cannot cast from View to TextView. Have had a quick look into it with no luck but Im currently trying to solve the first and more important problem!
Thanks in advance,
Josh