Android listview, current item counter
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_开发者_JAVA百科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>
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.
精彩评论