How to correctly set multiple tag values for a view in Android

Source: Internet
Author: User

During Android development, we often use the View.settag (object) method, especially when reusing view in a custom adapter of a ListView. The view also provides a way to set up multiple tag data, View.settag (Int,object), where the int value needs to be set correctly, otherwise this method will give an error. Let's see how to set this value. Look at the results first:

As you can see from the Eclipse print log, I set the 4 different types of tag values for the GridView. Take a look at the code again.

 PackageCom.androidtest.scroll;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.AbsListView;ImportAndroid.widget.AbsListView.OnScrollListener;ImportAndroid.widget.Button;ImportAndroid.widget.GridView;ImportAndroid.widget.TextView;ImportCOM.ANDROIDTEST.R;Importcom.androidtest.base.BaseActivity; Public  class gridviewactivity extends baseactivity  implements  Onclicklistener{    PrivateTextView title;PrivateGridView GridView;PrivateButton Btn_settag,btn_gettag;Private Final Static intFive=5<< -;Private Final Static intsix=6<< -;Private Final Static intserven=7<< -;Private Final Static inteight=8<< -;@Override    protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub        Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_gridview);    Init (); }Private void Init() {title= (TextView) Findviewbyid (r.id.title); Title.settext ("Test GridView"); Findviewbyid (R.id.back). Setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {//TODO auto-generated method stubFinish ();        }        });        Rootview=findviewbyid (R.id.rootview);        Btn_gettag= (Button) Findviewbyid (R.id.btn_gettag);        Btn_settag= (Button) Findviewbyid (R.id.btn_settag); Btn_settag.setonclicklistener ( This); Btn_gettag.setonclicklistener ( This);        gridview= (GridView) Findviewbyid (R.id.gridview);    Gridview.setadapter (Createadapter ()); }@Override     Public void OnClick(View v) {//TODO auto-generated method stub        if(V==btn_gettag)        {Gettag (); }Else if(V==btn_settag)        {Settag (); }    }Private void Settag() {Gridview.settag (five,"Hello World"); Gridview.settag (Six,true); Gridview.settag (Serven, -); Gridview.settag (Eight,158.9f); }Private void Gettag() {LOG.E ("GridView Gettag (Index)","String:"+ (String) Gridview.gettag (five)); LOG.E ("GridView Gettag (Index)","Boolean:"+ ((Boolean) Gridview.gettag (six)); LOG.E ("GridView Gettag (Index)","int:"+ ((Integer) Gridview.gettag (Serven))); LOG.E ("GridView Gettag (Index)","float:"+ (Float) Gridview.gettag (eight)); }}

Operating interface:

The int value in View.settag (Int,object) must be shifted to the left by 24 bits, so that no error will be done.

How to correctly set multiple tag values for a view in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.