0

I want to make a ListItem selected as app starts and get the text/value of selected item,but I'm unable to do.

Code

public class MenuList extends ListActivity {

String[] classNames = {"MainActivity", "example"}; 
//private View currentSelectedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(MenuList.this, android.R.layout.simple_list_item_1, classNames));

}   
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);      
    String itemText= classNames[position];

    Toast.makeText(MenuList.this, itemText, Toast.LENGTH_LONG).show();      
}       
  }

through this I get the text/value of listItem but Im unable to make a listItem pre-selected. can any-one tell me how to do so..?

4

1 回答 1

0

像这样将选择器设置为ListView添加android:background="@drawable/list_bg"到 Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_bg" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

list_bg.xml并像这样在你的drawable中创建

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/grey" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@color/blue" android:state_pressed="true"/>
    <item android:drawable="@color/blue" android:state_pressed="false" android:state_selected="true"/>

</selector>
于 2013-02-03T12:17:46.520 回答