默认情况下,在ListView中选择一个行,然后对选中的Android行高亮显示 [英] Default Selection of a Row in the ListView and HighLight the selected Row Android
问题描述
我有2个列表视图.我想在活动开始时默认选择两个listview的第一行.我在google上找到了
I have 2 listview. I want when the activity starts first row of the both the listview to be selected by default.I googled up i found this
onewaydata=new OneWayFlightResult(this, flightData);
onewayListView.setAdapter(onewaydata);
onewayListView.setSelection(0);
onewayListView.getSelectedView().setSelected(true);
但这对我不起作用
这是我拥有的两个ListView
These are the two ListView that i have
<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
And then the 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
所以请建议我要做的事情才能在列表视图上进行默认选择.
SO please suggest me what i have to do to make the default selection on the listview.
推荐答案
尝试一下,它应该可以解决问题:
Try this, it should do the trick:
your_list_view.post(new Runnable() {
@Override
public void run() {
your_list_view.setSelection(0);
}
});
更新:请查看此内容
class YourAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
View view = convertView;
if(position == 0){
// This is the first item, you need to select this
view.setSelected(true);
}
// Do whatever you want here
return view;
}
}
在XML中:
<ListView
android:id="@+id/lvDepartures"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:listSelector="@null" <!--Here is the thing you need-->
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:listSelector="@null" <!--Here is the thing you need-->
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
在您的item-row-xml中:
In your item-row-xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/item_row_selector"> <!--Here is the thing you need-->
<!--Your item layout here-->
</LinearLayout>
最后,您需要以"selected"状态实现自己的item_row_select.
And last you need to implement your own item_row_select with the state of "selected".
希望这会有所帮助.
这篇关于默认情况下,在ListView中选择一个行,然后对选中的Android行高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!