1

ListActivityを拡張するクラスがあり、正常に機能します

次に、onListItemClick()でgetSelectedItemPosition()を使用すると、常に-1が返されます。

PS getSelectedItemId()は、994393434のような長い数値を返します

public class TasksShowActivity  extends ListActivity {

    private Cursor mCursor; 
    private ListAdapter mAdapter;

    private static final String[] mContent = new String[] {
        TasksDbHelper._ID, TasksDbHelper.NAME,
        TasksDbHelper.USER};

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     

        mCursor = managedQuery(
                TasksProvider.CONTENT_URI, mContent, null, null, null);

        mAdapter = new SimpleCursorAdapter(this, 
                    R.layout.tasks, mCursor, 
                    new String[] {TasksDbHelper.NAME, TasksDbHelper.USER}, 
                    new int[] {R.id.name, R.id.date});

        setListAdapter(mAdapter);
    }

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

      Toast toast = Toast.makeText(this, "Position: "+this.getSelectedItemPosition() , Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
    }
}

レイアウト:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tasks_root_element">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:textSize="18sp"
        />
    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true"
        android:textSize="18sp" 
        android:paddingRight="10px"
        />

</RelativeLayout>
4

1 に答える 1

10

次に、onListItemClick() で getSelectedItemPosition() を使用し、常に -1 を返します

これは、何も選択されていないためです。「クリック」と「選択」は別物です。での「選択」はListView、ポインティング デバイス (方向パッド、トラックボール、矢印キーなど) を介して行われます。

于 2012-05-23T17:07:13.307 に答える