Converting Int to String!

Asked

Viewed 2,154 times

2

I am trying to recover some information recorded in the database, only when an error happens, because I am trying to take an "integer" value and pass to "String", then I am not able to do this conversion, follow below the codes:

public class ProdutoAdapter extends ArrayAdapter<Produto> {

private final Context context;
private final ArrayList<Produto> elementos;

public ProdutoAdapter(Context context, ArrayList<Produto> elementos) {
    super(context, R.layout.linha_produtos, elementos);
    this.context = context;
    this.elementos = elementos;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.linha_produtos, parent, false);
    TextView id = (TextView) rowView.findViewById(R.id.txtId);
    TextView descricao = (TextView) rowView.findViewById(R.id.txtDesc);
    TextView preco = (TextView) rowView.findViewById(R.id.txtVVenda);
    TextView estoque = (TextView) rowView.findViewById(R.id.txtEstoque);
    id.setText(Integer.parseInt(elementos.get(position).getId()));
    descricao.setText(elementos.get(position).getDescricao());
    preco.setText(elementos.get(position).getValorVenda());
    estoque.setText(elementos.get(position).getEstoqueAtual());
    return rowView;
}

The mistake happens here id.setText(Integer.parseInt(elementos.get(position).getId()));

Here is the error code:

  android.content.res.Resources$NotFoundException: String resource ID #0x1
    at android.content.res.Resources.getText(Resources.java:338)
    at android.widget.TextView.setText(TextView.java:5494)
    at com.example.rodrigoconceicao.controleestoque2_1.ProdutoAdapter.getView(ProdutoAdapter.java:32)

Note: The "ID" object is declared as "INT" in the class.

2 answers

6


The method parseInt() class Integer receives a String and converts it into a primitive value of the int. As you said yourself id is declared as a int in class Produto, the call Integer.parseInt(elementos.get(position).getId()) does not make much sense, since 1) you are passing a int for a method that waits String as parameter and 2) you would be trying to convert a int in a int.

To convert a value of type int in String, you need the method valueOf(), which, among other types, may receive a int and convert it into String:

id.setText(String.valueOf(elementos.get(position).getId()));

So to summarize, when you want to convert between types, the class you should use is the one you want your value to be converted to String, you will use class conversion methods String; if you want your value converted to double, will use class methods Double etc..

  • It worked, thank you for your attention!

  • If this answer answered your question, click the tick symbol to mark it as correct and help other users.

  • What symbol would that be, so I can click???

  • 1

    On the upper left side next to the answer, in gray, a symbol in the format of "right" which, when clicked, is in green. Accepting an answer as correct helps other users who have their doubts have confidence that that answer solved their problem and may solve theirs as well.

  • Got it and done, thanks!

0

There are two ways to solve this problem:

1 - You can use the String.valueOf:

id.setText(String.valueOf(elementos.get(position).getId()));

2 - You can use concatenation:

id.setText("" + Integer.parseInt(elementos.get(position).getId()));

Of course, the first option is the right one to use, but nothing prevents you from using the second.

  • It worked out thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.