Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden Field/Tag in TextView?

I want to pass a value that will be generated at runtime,through a TextView. the text property is used for some other data and the data that I want to pass will not be displayed. So, it's a like a hidden tag. Is it possible to do with TextView? If so, which property of the TextView.

For simplicity's sake imagine I pull the ID and TEXT from the data table. Now the TEXT is displayed on the TextView but when I want to pass the reference to that particular row of the table to some other function I want to pass the ID as an argument/handle. So, the ID will be hidden and associated with the TextView. How can I do it? If not possible can you suggest any alternative to accomplish this? BTW, the TextView is embedded within a ListView.

Adapter code :

cursor = db.rawQuery("SELECT * FROM EmpTable", null);  adapter = new SimpleCursorAdapter(                 this,                  R.layout.item_row,                  cursor,                  new String[] {"Emp_Name"},                  new int[] {R.id.txtEmployee}); 
like image 254
redGREENblue Avatar asked Mar 09 '11 20:03

redGREENblue


1 Answers

Try setTag(int, Object) and getTag(int). There are even versions that don't take a key, if you just want to store one value. From the docs:

Sets the tag associated with this view. A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.

So you can do:

textView.setTag(myValue); 

and get it back later with:

myValue = textView.getTag(); 

Since the interface uses Object, you will need to add casts. For example, if your value is an int:

textView.setTag(Integer.valueOf(myInt)); 

and:

myInt = (Integer) textView.getTag(); 

Edit - to subclass and add the tag, use:

adapter = new SimpleCursorAdapter(this, R.layout.item_row,         cursor, new String[] {"Emp_Name"}, new int[] R.id.txtEmployee}) {     @Override     public View getView (int position, View convertView, ViewGroup parent) {         View view = super.getView(position, convertView, parent);         view.setTag(someValue);         return view;     } }; 
like image 101
Matthew Willis Avatar answered Sep 26 '22 04:09

Matthew Willis