简体   繁体   中英

Comparing button Tag with String in android

I am working on an android project where it needs to get data from database as grid view (contains multiple buttons). I have already done up to that part. Now I need to compare those data with a given string. Here, I have tagged the status from the database to the button before put it in to the grid view.

holder.btn.setTag(data.get(position).getStatus());

the following code has shown that how I am trying to compare those values.

    String x = "NA";
    String y = holder.btn.getTag().toString();

    if (x.equals(y)) {
        holder.btn.setEnabled(false);
    }

But it is not working. Please Help me to solve this issue.

Additionally, in my database there is a column call status....it contains values such as A and NA (Available and Not Available). I have already got that values from data base and set it to the item objects array list call data. in that item object i have declared field call status and then I have assigned that data base values to that.

Thanks in advance.

The method getTag() returns an Object , not a String . So you have to cast it to a String when you retrieve it. Try this:

String x = "NA";
String y = (String) holder.btn.getTag();

if (x.equals(y)) {
    holder.btn.setEnabled(false);
}

Also keep in mind that if you are originally setting the Tag with something other than a String, then you'll have to convert it to a String when you use getTag() . So, for example, if your line holder.btn.setTag(data.get(position).getStatus()); sets an int as the tag, you would have to do String y = (String) Integer.toString(holder.btn.getTag()); or something similar.

Maybe the problem is at line if getStatus() returns no string

holder.btn.setTag(data.get(position).getStatus());

You can try with

holder.btn.setTag(data.get(position).getStatus().toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM