在setOnItemClickListener(ListView)中找不到OnItemClickListener

时间:2013-04-30 15:50:39

标签: android android-listview

我是Android的新手,但我希望我能做出一个真正轻松的错误:)

我想添加mainListView.setOnItemClickListener,但我会收到错误

"The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView<?>, View, int, long)"
at OnItemClickListener()
视图中的

和“View cannot be resolved to a type”。

也许有一个简单的解决方案。希望如此。提前致谢!

public class MainActivity extends Activity {

        private ListView mainListView ;  
        private ArrayAdapter<String> listAdapter ;  

        // ...

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // ...

            // Find the ListView resource.   
            mainListView = (ListView) findViewById( R.id.mainListView );  

            ArrayList<String> outputList = new ArrayList<String>();  
            // Create ArrayAdapter using the planet list.  
            listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, outputList);  

            mainListView.setAdapter( listAdapter );   
            mainListView.setTextFilterEnabled(true);
            mainListView.setOnItemClickListener(new OnItemClickListener() {

                @Override

                public void onItemClick(AdapterView<?>  parent, View view, int position, long id) {

                  // Do something

                }

            });
        }

        // ...
    }

编辑:我将代码更改为:

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?>  parent, View view, int position, long id) {

      // Do something like this
        AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
        adb.setTitle("ListView OnClick");
        adb.setMessage("Selected Item is = "
        + mainListView.getItemAtPosition(position));
        adb.setPositiveButton("Ok", null);
        adb.show();

    }

});

现在它有效。感谢

我可以查看评论作为解决方案吗?

0 个答案:

没有答案