2

我的 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

4

3 回答 3

4

First, this is the code I modified and it worked.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parnet, android.view.View view,
            int position, long id) {
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                Toast.LENGTH_SHORT).show();
    }

});

The problem here is your "class" name, you've chosen "View" as your class name, which happens to be the same as android.view.View, so if you use View as the type of the second parameter of your listener, the compiler gets confused.

My suggestion is that, you'd better choose another name for your activity class. I usually use the functionality of the activity plus a "Activity" postfix as activity class name, for example, if you want to display a list of users, you can choose UserListActivity, here I choose the "ListActivity" as the postfix to indicate that the activity actually derives from ListActivity.

于 2012-04-18T00:52:45.653 回答
0

Can you try adding:

@Override

above onItemClick():

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
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();
}

});
于 2012-04-18T00:45:54.393 回答
0

View is already a class that exists, try renaming it to something else....

于 2012-04-18T01:12:55.837 回答