安卓:列表视图中选择项目-1 [英] Android: List View Selected item -1

查看:135
本文介绍了安卓:列表视图中选择项目-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时得到-1值时,我试图让我的列表视图已填充已选定的项目位置。

Im getting a -1 value when i try to get the selected item position on my listview that is already populated.

list.setOnItemClickListener
(
   new AdapterView.OnItemClickListener() 
   {
       public void onItemClick(AdapterView adapterView, View view,int arg2, long arg3)
       {
          int selectedPosition = adapterView.getSelectedItemPosition();
          ShowAlert(String.valueOf(selectedPosition));
       }
   }
);

要填补我的列表视图我用下面的code:

To fill my list view i use the following code:

SimpleAdapter mSchedule = new SimpleAdapter(
          this, 
          mylist, 
          R.layout.listviewtest,
          new String[] {"test1", "test2", "test3"}, 
          new int[] {R.id.TextView_websitename, R.id.TextView_keywords, R.id.TextView_backlink});

你知道吗?

在此先感谢。

最好的问候。

何塞。

推荐答案

这意味着没有选择的行。该<一href="http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition%28%29">documentation指出 getSelectedItemPosition()返回:

That means there is no row selected. The documentation states that getSelectedItemPosition() returns:

INT位置(从0开始),或   INVALID_POSITION如果没有什么   选中。

int Position (starting at 0), or INVALID_POSITION if there is nothing selected.

INVALID_POSITION 是<一个href="http://developer.android.com/reference/android/widget/AdapterView.html#INVALID_POSITION"><$c$c>-1.

请注意,您所呼叫 getSelectedItemPosition() OnClickListener 。单击并选择没有必然关系。选择来自使用方向键或轨迹球导航列表内容。如果用户点击屏幕上(或者点击了仿真器),就没有选择了,但还是会有一个click事件。

Note that you are calling getSelectedItemPosition() from an OnClickListener. Click and selection are not necessarily related. Selection comes from using the D-pad or trackball to navigate the list contents. If the user taps on the screen (or clicks in the emulator), there will be no selection anymore, but there will still be a click event.

ARG2 值你展示的是位置被点击后,在列表中的项目。

The arg2 value you show is the position of the clicked-upon item in the list.

这篇关于安卓:列表视图中选择项目-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆