Android: onListItemClick not getting called in ListActivity

筅森魡賤 提交于 2019-12-23 05:36:14

问题


I'm having problems with my first Android app.

I have subclassed ListActivity, and I'm having no luck getting the overridden onListItemClick() to respond to click events. I've read focus can be a problem, but changing focus in the XML files does not seem to work. Here's the relevant bits of code. Anyone see what's I've buggered up?

public class Notepadv1 extends ListActivity {
    private int mNoteNumber = 1;
    private NotesDbAdapter mDbHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notepad_list);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        fillData();
    }

 private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor c = mDbHelper.fetchAllNotes();
    startManagingCursor(c);

    String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
    int[] to = new int[] { R.id.text1 };


    SimpleCursorAdapter notes =
        new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
    setListAdapter(notes);
}

@Override
public void onListItemClick (ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);

    AlertDialog alert = new AlertDialog.Builder(this).create();
    String message = "row clicked!";
    alert.setMessage(message);
    alert.show();

}

notepad_list.xml

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

  <ListView
      android:id="@android:id/list"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:dividerHeight="6dp"/>



  <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_notes" />

</LinearLayout>

And notes_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:focusable="false"/>

回答1:


I created a ListActivity, used your onListItemClick, and notes_row.xml. The code works for me. However, I believe you are expecting the entire row to respond to a click, according to notes_row you need to click on the text itself (not anywhere in the row).

Try changing the width attribute in notes_row.xml to "match_parent":

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_height="60dp"
    android:layout_width="match_parent"
    android:focusable="false" />

Now the entire row responds to user clicks.




回答2:


Another potential solution in case this helps anyone: I had a Button in each of my list items, and this prevented my onListItemClick event from firing. Replacing the Button with a TextView that looked exactly the same made it work.




回答3:


Implements OnItemClickListener and do list.setOnItemClickListener.

public class Notepadv1 extends ListActivity implements OnItemClickListener { 

   onCreate() 
   {
        ....

       ((ListView)findViewById(android.R.id.list)).setOnItemClickListener(this)

    }

}


来源:https://stackoverflow.com/questions/11369382/android-onlistitemclick-not-getting-called-in-listactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!