简体   繁体   中英

Android GridView setSelection

When a Fragment is inflated, by default I want to item no 10 to be selected. I tried it with gridview.setSelection(10); but it is not working for me. I read in the forum that others have done it successfully , but I am not achieving the correct output. what should I do?

orari_fine = (GridView) findViewById(R.id.gridView4);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, orari_f);
orari_fine.setAdapter(adapter1);
orari_fine.setSelection(10);
orari_fine.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
         OraFine = ((TextView) v).getText().toString();

    }
});

XML

<GridView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:listSelector="@drawable/selector"
    android:id="@+id/gridView4"
    android:background="#9051585B"
    android:choiceMode="singleChoice"
    android:numColumns="8"
    android:textAlignment="center"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/textView13" />

instead of using gridview.setSelection(10); try using gridview.setItemChecked(10, true);

try do this

   // set item postion you wanna select
    orari_fine.setSelection(10);  
    orari_fine.setFocusableInTouchMode(true);  
    orari_fine.requestFocus();
// Notifies the attached observers that the underlying data has been changed
    adapter1.notifydatasetchanged();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM