我在我的自定义ListView适配器中使用此代码(我删除了几行代码,它基本上是我对变更所做的,但它并不涉及将其转换为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;
}
}
&#13;
为什么changenum = Integer.parseInt(change);
无效。我事先尝试过宣布private String change
,但它仍然无法正常工作。更改字符串是小数部分,其中数字除以&#34;。&#34; (例如2.5),是造成问题吗?如果不是,那是什么?
答案 0 :(得分:1)
您需要解析为Float
或Double
而不是Int
-
例如:changenum = Double.parseDouble(change);