我有 2 个列表视图。我想在活动开始时默认选择两个列表视图的第一行。我用谷歌搜索我发现了这个
onewaydata=new OneWayFlightResult(this, flightData);
onewayListView.setAdapter(onewaydata);
onewayListView.setSelection(0);
onewayListView.getSelectedView().setSelected(true);
但这对我不起作用
这些是我拥有的两个 ListView
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/footerLayout"
android:layout_below="@+id/sortFlightLayouts"
android:orientation="horizontal" >
<ListView
android:id="@+id/lvDepartures"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray" />
<ListView
android:id="@+id/lvArrivals"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="@color/change_text_color"
android:layout_height="wrap_content" >
</RelativeLayout>
然后是change_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#E5FFED" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#E5FFED" /> <!-- focused -->
<item android:color="#EEE9E9" /> <!-- default -->
</selector>
它正在抛出异常
08-07 15:58:45.138: E/AndroidRuntime(3152): FATAL EXCEPTION: main
08-07 15:58:45.138: E/AndroidRuntime(3152): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gridtestproject/com.example.gridtestproject.TestActivity}: android.view.InflateException: Binary XML file line #217: Error inflating class android.widget.ListView
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.ActivityThread.access$600(ActivityThread.java:149)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.os.Looper.loop(Looper.java:153)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.ActivityThread.main(ActivityThread.java:4987)
08-07 15:58:45.138: E/AndroidRuntime(3152): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 15:58:45.138: E/AndroidRuntime(3152): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 15:58:45.138: E/AndroidRuntime(3152): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-07 15:58:45.138: E/AndroidRuntime(3152): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-07 15:58:45.138: E/AndroidRuntime(3152): at dalvik.system.NativeStart.main(Native Method)
08-07 15:58:45.138: E/AndroidRuntime(3152): Caused by: android.view.InflateException: Binary XML file line #217: Error inflating class android.widget.ListView
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
08-07 15:58:45.138: E/AndroidRuntime(3152): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-07 15:58:45.138: E/AndroidRuntime(3152): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.Activity.setContentView(Activity.java:1867)
08-07 15:58:45.138: E/AndroidRuntime(3152): at com.example.gridtestproject.TestActivity.onCreate(TestActivity.java:34)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.Activity.performCreate(Activity.java:5020)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
08-07 15:58:45.138: E/AndroidRuntime(3152): ... 11 more
08-07 15:58:45.138: E/AndroidRuntime(3152): Caused by: java.lang.reflect.InvocationTargetException
08-07 15:58:45.138: E/AndroidRuntime(3152): at java.lang.reflect.Constructor.constructNative(Native Method)
08-07 15:58:45.138: E/AndroidRuntime(3152): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
08-07 15:58:45.138: E/AndroidRuntime(3152): ... 25 more
08-07 15:58:45.138: E/AndroidRuntime(3152): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/item_selected.xml from drawable resource ID #0x7f02000c
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.content.res.Resources.loadDrawable(Resources.java:1959)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.View.<init>(View.java:3343)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.view.ViewGroup.<init>(ViewGroup.java:433)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.widget.AdapterView.<init>(AdapterView.java:235)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.widget.AbsListView.<init>(AbsListView.java:781)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.widget.ListView.<init>(ListView.java:147)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.widget.ListView.<init>(ListView.java:143)
08-07 15:58:45.138: E/AndroidRuntime(3152): ... 28 more
08-07 15:58:45.138: E/AndroidRuntime(3152): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
08-07 15:58:45.138: E/AndroidRuntime(3152): at android.content.res.Resources.loadDrawable(Resources.java:1956)
08-07 15:58:45.138: E/AndroidRuntime(3152): ... 35 more
所以请建议我如何在列表视图上进行默认选择。