0

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

1
  • Are you clicking on the button or an item in the list? How are you filling the list? Commented Mar 10, 2011 at 16:42

3 Answers 3

1

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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It turns out that the problem was in my layout file (xml) which provides the definition for the row in the list. I had a ScrollLayout set where it should have been LinearLayout.
0

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?

Comments

0

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

1 Comment

He's not showing a context menu. That's also for a Long Click.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.