Android列表视图,当前项目计数器
在我的 Android 应用程序中,我有一个列表视图,它根据用户查询显示动态数据。
问题是我需要在页脚中显示用户当前选择的项目(在滚动列表视图时)。
我实现了 OnItemSelectedListener 并可以捕获滚动位置。 我最初定义了一个全局变量并将其指定为-1。
int selectedRowIndex = -1;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
selectedRowIndex = arg2;
footer.setText(String.valueOf(selectedRowIndex+1)+ " of " + String.valueOf(companyList.size()));
}
问题是,当页面最初加载时,甚至还没有选择任何项目(滚动),selectedRowIndex 值已变为 0(列表视图中的第一个项目)。我无法理解为什么会发生这种情况,任何人都可以建议一种方法来实现这一点。
提前致谢
编辑
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
<TextView android:text="TextView"
android:id="@+id/noCompanysFound_Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:textStyle="italic"
android:layout_marginTop="10dp">
</TextView>
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/companylistView"
android:listSelector="@drawable/item_focus_bkg"
android:layout_above="@+id/footer_linearLayout"
android:focusable="false">
</ListView>
<LinearLayout android:id="@+id/footer_linearLayout" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="@drawable/bottom_bkg" android:gravity="center_horizontal">
<TextView android:text="Footer Text"
android:id="@+id/companylistFooterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</RelativeLayout>
In my android application I have a listview which displays dynamic data according to the user query.
The problem is I need to display in the footer, which Item the user currently have selected(while Scrolling through the listview).
I Implemented OnItemSelectedListener
and can catch the scrolling position.
I initially defined a global variable and asigned it to -1.
int selectedRowIndex = -1;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
selectedRowIndex = arg2;
footer.setText(String.valueOf(selectedRowIndex+1)+ " of " + String.valueOf(companyList.size()));
}
The problem is, when the page initially load and even no item have yet selected(Scroll), the selectedRowIndex value had become 0(The first Item in the listview). I cant understand why it happen and can anyone suggest a way to implement this .
Thanks inadvance
Edits
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
<TextView android:text="TextView"
android:id="@+id/noCompanysFound_Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:textStyle="italic"
android:layout_marginTop="10dp">
</TextView>
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/companylistView"
android:listSelector="@drawable/item_focus_bkg"
android:layout_above="@+id/footer_linearLayout"
android:focusable="false">
</ListView>
<LinearLayout android:id="@+id/footer_linearLayout" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="@drawable/bottom_bkg" android:gravity="center_horizontal">
<TextView android:text="Footer Text"
android:id="@+id/companylistFooterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为每当你的活动创建你的
listview获得焦点
并且第一行将被选择
这就是为什么listview的onItemSelected()
肯定会被调用一次并且列表第一行的索引是 0,所以在代码中如果我错了,请给我建议或让我知道。
I think whenever your activity created your
listview get Focused
and thefirst row will be selected
that's whylistview's onItemSelected()
will be surely called once and the index of first row of list is 0 so in codeIf I am wrong then give me suggestion or let me know.