Android GridView setSelection

时间:2015-05-21 16:08:30

标签: android gridview selection selected

当片段膨胀时,默认情况下我想要选择项目编号10。 我用gridview.setSelection(10);尝试了它,但它不适用于我。我在论坛上看到其他人已成功完成,但我没有达到正确的输出。我该怎么办?

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" />

2 个答案:

答案 0 :(得分:0)

而不是使用gridview.setSelection(10);尝试使用gridview.setItemChecked(10, true);

答案 1 :(得分:0)

尝试这样做

   // 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();