3

我有 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

所以请建议我如何在列表视图上进行默认选择。

4

3 回答 3

19

而不是 your_list_view.setSelection(0);
试试 your_list_view.setItemChecked(0,true);

于 2014-05-26T15:53:16.663 回答
1

试试这个,它应该可以解决问题:

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 中:

    <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 状态为“已选择”。

希望这可以帮助。

于 2013-08-07T09:35:20.210 回答
0
**THIS WORKS FOR ME.You have to make custom adapter.**

     First Create Modal Class

        public class FilterBean {

         String filter_catagory;
                 boolean isSelected;

            public boolean isSelected() {
                return isSelected;
            }

            public void setSelected(boolean selected) {
                isSelected = selected;
            }

          public String getFilter_catagory() {
                return filter_catagory;
            }

            public void setFilter_catagory(String filter_catagory) {
                this.filter_catagory = filter_catagory;
            }

        }


    In Adapter Class

    @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = layoutInflater.inflate(R.layout.size_name_indicator, null);
            txt = (TextView) convertView.findViewById(R.id.key_textView);
            txt.setText(getItem(position).getFilter_catagory());

            if(getItem(position).isSelected()){
                txt.setBackgroundColor(context.getResources().getColor(R.color.transparent));
            }else {

                txt.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
            }


            return convertView;
        }

     public void updateNameBean(String key,boolean checked)
        {

            for(int i=0;i<getCount();i++)
            {
                FilterBean filterBean =  getItem(i);
                if(filterBean.getFilter_catagory().equals(key))
                {
                    filterBean.setSelected(checked);
                }else {
                    filterBean.setSelected(false);
                }
            }
            notifyDataSetChanged();
        }

**In Activity or Fragment**

 filter_listView.setAdapter(filterNameAdapter);
               filterNameAdapter.updateNameBean(filterNameAdapter.getItem(0).getFilter_catagory(),true);
        filter_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String key = filterNameAdapter.getItem(position).getFilter_catagory();
                filterNameAdapter.updateNameBean(filterNameAdapter.getItem(position).getFilter_catagory(),true);

            }
        }); 
于 2016-10-17T08:30:46.230 回答