Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OnItemClickListener not working

I'm using android 2.3.3. I set up a layout like,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView android:id="@+id/mainList" android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

And I'm manipulating it with

package org.dewsworld.ui;

import org.dewsworld.core.NBConfig;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class NewsBotActivity extends ListActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setListAdapter( new ArrayAdapter<String>(this,
                                            android.R.layout.simple_list_item_1,
                                            NBConfig.topics));

        ListView listView = (ListView) findViewById(R.id.mainList) ;
        listView.setOnItemClickListener( new OnItemClickListener() {

        });
    }
}

Using eclise IDE, when I setthe OnItemClickListener it gives me the error

The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new 
     OnItemClickListener(){})

I can't fix this. [I've added an image with the error]imgimg

like image 290
Dewsworld Avatar asked Sep 28 '11 02:09

Dewsworld


3 Answers

It seems you have imported the wrong OnItemClickListener, try this one instead, and remove import of android.view.View.OnClickListener

import android.widget.AdapterView.OnItemClickListener;
like image 122
ZelluX Avatar answered Nov 13 '22 11:11

ZelluX


how about filling in the body of your new object by defining the onItemClick() function:

public void onItemClick(AdapterView parent, View v, int position, long id)
{
    // Display a messagebox.
    Toast.makeText(this,"Your Listener Works!",Toast.LENGTH_SHORT).show();
}

try using ctrl+shift+o in eclipse to organize all your imports automatically...

like image 42
blad Avatar answered Nov 13 '22 11:11

blad


Just implement OnItemClickListener to your class.

Like This:

public class ClassName extends Activity implements OnItemClickListener{}

like image 39
Andy Avatar answered Nov 13 '22 12:11

Andy