Android列表视图,当前项目计数器

发布于 2024-12-10 02:35:32 字数 2344 浏览 5 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九局 2024-12-17 02:35:32

我认为每当你的活动创建你的listview获得焦点并且第一行将被选择这就是为什么listview的onItemSelected()肯定会被调用一次并且列表第一行的索引是 0,所以在代码中

selectedRowIndex = arg2; became selectedRowIndex = 0;  

如果我错了,请给我建议或让我知道。

I think whenever your activity created your listview get Focused and the first row will be selected that's why listview's onItemSelected() will be surely called once and the index of first row of list is 0 so in code

selectedRowIndex = arg2; became selectedRowIndex = 0;  

If I am wrong then give me suggestion or let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文