eljainc
eljainc

Reputation: 117

Android ListView - click handler not working

I'm still having a problem with a ListView click handler. The event is not being fired, so I do not see the debugging message.

I want to use Activity instead of ListActivity

Here again is my code:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    // Capture our button from layout
    //appState = ((MyApp)getApplicationContext());
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    //requestWindowFeature(Window.FEATURE_LEFT_ICON);




    setContentView(R.layout.listr);


    //setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.fishicon);

   setupDB();  
   populateList3();



   ListView lv = (ListView) findViewById(R.id.list);


  // lv.setClickable(true);


     lv.setOnItemClickListener(new OnItemClickListener() {

       //@Override

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

           Toast.makeText(ListRecords.this,"Clicked!", Toast.LENGTH_LONG).show();
       }
   });

Here is the XML file:

<?xml version="1.0" encoding="utf-8"?>  
  <LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
     android:layout_width="fill_parent"  
     android:orientation="vertical"  
     android:gravity="center"
     android:layout_height="fill_parent">  



  <!--Put form controls here-->  
 <ListView 
    android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="400dp" /> 
     <Button
        android:id="@+id/previousbutton"
        android:gravity="center_horizontal"
        android:layout_width = "fill_parent"
        android:layout_height = "fill_parent"

        android:text="Previous Menu"/>
</LinearLayout>  

I cannot figure out why the click listener is not working.

Thanks Mike

Upvotes: 0

Views: 3686

Answers (3)

Squonk
Squonk

Reputation: 48871

Don't use inline listeners unless you're sure of how to use them - they can go out of scope and cause a variety of problems.

Try this...

public class MyActivity extends Activity
    implements AdapterView.onItemClickListener {

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

        ListView lv = (ListView) findViewById(R.id.list);
        lv.setOnItemClickListener(this);

        // Do whatever else you need to do
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Do whatever
    }
}

Upvotes: 1

Leif Andersen
Leif Andersen

Reputation: 22332

You're not registering the list for the context menu: http://developer.android.com/reference/android/app/Activity.html#registerForContextMenu(android.view.View)

Upvotes: 0

Matthew
Matthew

Reputation: 44919

I would uncomment the @Override. It is there to guarantee that your signature matches the method you're trying to override.

If your method signature is correct, then I suspect the problem lies in populateList3(). Could you post that code as well?

Upvotes: 0

Related Questions