Kazhiunea
Kazhiunea

Reputation: 11

Can't turn string into int

I am using this code in my custom ListView adapter (I have removed few lines of code, it's basically what I'm doing with change, but it doesn't involve turning it into int):

public class ShowcaseAdapter extends ArrayAdapter<Coin>{
        private static final String TAG = "PersonalListAdapter";

        private Context mContext;
        int mResource;

    public ShowcaseAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Coin> objects) {
        super(context, resource, objects);
        this.mContext = context;
        mResource = resource;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String change = getItem(position).getChange();

        Coin coin = new Coin(change);

        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);

        TextView tvChange = (TextView) convertView.findViewById(R.id.change_box);

        int changenum;
        changenum = Integer.parseInt(change);

        if(changenum >= 0){
            tvChange.setTextColor(Color.GREEN);
        }
        else{
            tvChange.setTextColor(Color.RED);
        }
        tvChange.setText(change);

        return convertView;
    }
}

Why is the changenum = Integer.parseInt(change); doesn't work. I've tried declaring private String change beforehand, but it still doesn't work. The change String is a decimal fraction, where the numbers are divided with "." (ex. 2.5), is that causing problems? If no, what is?

Upvotes: 0

Views: 54

Answers (1)

Mithilesh Gupta
Mithilesh Gupta

Reputation: 2930

You need to parse to Float or Double instead of Int -

Eg: changenum = Double.parseDouble(change);

Upvotes: 1

Related Questions