在ListView一排的默认选择,彰显slected行的Andr​​oid [英] Default Selection of a Row in the ListView and HighLight the slected Row Android

查看:184
本文介绍了在ListView一排的默认选择,彰显slected行的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2列表视图。我想,当活动开始的两个列表视图的第一行被default.I选择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);

但是,这不是为我工作。

But this is not working for me

这是两个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"/>

在您的项目排-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>

和最后你需要用选择的状态,以实现自己的item_row_select。

And last you need to implement your own item_row_select with the state of "selected".

希望这有助于。

这篇关于在ListView一排的默认选择,彰显slected行的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆