Skip to content Skip to sidebar Skip to footer

Default Selection Of A Row In The Listview And Highlight The Selected Row Android

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, f

Solution 1:

instead of your_list_view.setSelection(0); try your_list_view.setItemChecked(0,true);

Solution 2:

Try this, it should do the trick:

your_list_view.post(newRunnable() {

        @Overridepublicvoidrun() {
           your_list_view.setSelection(0);          
           }
        });

UPDATE: Please take a look at this

classYourAdapterextendsBaseAdapter{

            @OverridepublicintgetCount() {
                // TODO Auto-generated method stubreturn0;
            }

            @Overridepublic Object getItem(int arg0) {
                // TODO Auto-generated method stubreturnnull;
            }

            @OverridepubliclonggetItemId(int arg0) {
                // TODO Auto-generated method stubreturn0;
            }

            @Overridepublic View getView(int position, View convertView, ViewGroup arg2) {
                Viewview= convertView;

                if(position == 0){
                    // This is the first item, you need to select this
                    view.setSelected(true);

                }
                // Do whatever you want herereturn view;
            }

        }

In 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"/>

In your item-row-xml:

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

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

Hope this helps.

Solution 3:

**THISWORKSFORME.You have to make custom adapter.**

     FirstCreateModalClasspublicclassFilterBean {

         String filter_catagory;
                 boolean isSelected;

            publicbooleanisSelected() {
                return isSelected;
            }

            publicvoidsetSelected(boolean selected) {
                isSelected = selected;
            }

          publicStringgetFilter_catagory() {
                return filter_catagory;
            }

            publicvoidsetFilter_catagory(String filter_catagory) {
                this.filter_catagory = filter_catagory;
            }

        }


    InAdapterClass@OverridepublicViewgetView(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;
        }

     publicvoidupdateNameBean(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();
        }

**InActivity or Fragment**

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

            }
        }); 

Post a Comment for "Default Selection Of A Row In The Listview And Highlight The Selected Row Android"