Converting Int to String!

2

I'm trying to retrieve some information stored in the database, but when it's done an error occurs, since I'm trying to get an "integer" value and pass it to "String", there I can not do this conversion, 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 error 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.

    
asked by anonymous 04.12.2018 / 00:38

2 answers

6

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

For you to convert a value of type int to String , you need the valueOf() ", which, among other types, can receive int and convert it to String :

id.setText(String.valueOf(elementos.get(position).getId()));
So to sum up, when you want to do the conversion between types, the class you should use is the one you want your value to be converted to, ie: if you want its value converted to String , you will use conversion methods of class String ; if you want its value converted to double , it will use methods of class Double etc.

    
04.12.2018 / 11:22
0

There are two ways to resolve this problem:

1 - You can use 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 one.

    
04.12.2018 / 18:42