在活动中,它有一个ListView
,其中ListView
项目包含3个TextView
(monthAndYear,totalUsed(RM20)和预算(RM123.0))和一个{ {1}}。我尝试使用以下代码来显示ProgressBar
中的进度,但没有任何反应。
ProgressBar
适配器
public void retrieveList(String name) {
search.clear();
database = mdb.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE Name = ? ", new String[]{name}, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int iD = cursor.getInt(cursor.getColumnIndex("ID"));
String month = cursor.getString(cursor.getColumnIndex("Month"));
double budget = cursor.getDouble(cursor.getColumnIndex("Budget"));
double totalUsed=cursor.getDouble(cursor.getColumnIndex("Total_Used"));
if (adapter != null) {
adapter.add(iD,month,budget,totalUsed);
listview.setAdapter(adapter);
}
}
}
}
在 public void add(int id,String month,double budget,double used)
{
List obj = new List(id,month,budget,used);
obj.setMonthYear(" " + month);
obj.setBudget(budget);
obj.setUsed(used);
total = budget - used;
obj.setProgress(total);
search.add(obj);
this. notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder =null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.expenses_adapter, null);
holder= new ViewHolder();
holder.monthAndYear = (TextView) convertView.findViewById(R.id.monthAndYear);
holder.budget = (TextView) convertView.findViewById(R.id.budget);
holder.amount=(TextView)convertView.findViewById(R.id.amount);
holder.progressBar=(ProgressBar)convertView.findViewById(R.id.downloadProgressBar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.monthAndYear.setText(search.get(position).getMonthAndYear());
holder.budget.setText("RM"+""+search.get(position).getBudget());
holder.amount.setText("RM"+""+search.get(position).getUsed());
return convertView;
}
static class ViewHolder {
TextView monthAndYear, budget,amount;
ProgressBar progressBar;
}
之后添加holder.progressBar.setProgress(Integer.parseInt(total+""));
后,应用已崩溃。
错误
holder.amount.setText("RM"+""+search.get(position).getUsed());
答案 0 :(得分:5)
holder.progressBar.setProgress(Integer.parseInt(total+""));
这一行给你一个错误。你试图解析字符串是" 1.0"到整数,但值1.0是浮点类型。
试试这样: 取Float值并将其四舍五入。这将返回Integer
Math.round(Float.parseFloat(total+""))
答案 1 :(得分:2)
只需改变:
Integer.parseInt(total+"")
为:
(int) total
或者,如果你想更精确:
Math.round(total)
答案 2 :(得分:1)
你必须替换:
holder.progressBar.setProgress(Integer.parseInt(total+""));
使用:
holder.progressBar.setProgress(Math.round(total));
答案 3 :(得分:0)
值total
是double
或float
,setProgress()
方法以int
为参数。
因此,您可以执行以下操作,将total
的类型更改为int
,
或使用:
setProgress(Math.round(total));
Math.round
功能可以与double
或float
一起使用,如果您将int
传递给float
,它会重现long
,或者double
如果您将has_many :works, dependent: :destroy
def performers
works.map {|x| x.performer}
end
传递给它。输出将返回将输入四舍五入的值。