2016-12-30 177 views
-4

在活动中,它有一个ListView其中ListView项目由3 TextView(monthAndYear,totalUsed(RM20)和预算(RM123.0)),和ProgressBar。我尝试了下面的代码来显示ProgressBar的进度,但没有发生任何事情。设置进度,进度条

enter image description here

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.amount.setText("RM"+""+search.get(position).getUsed());之后添加holder.progressBar.setProgress(Integer.parseInt(total+""));,应用程序崩溃。

错误

java.lang.NumberFormatException: Invalid int: "1.0" 
      at java.lang.Integer.invalidInt(Integer.java:138) 
      at java.lang.Integer.parse(Integer.java:375) 
      at java.lang.Integer.parseInt(Integer.java:366) 
      at java.lang.Integer.parseInt(Integer.java:332) 
      at com.example.tony.monthlyexpenses.adapter.ExpensesAdapter.getView(ExpensesAdapter.java:106) 
      at android.widget.AbsListView.obtainView(AbsListView.java:2232) 

回答

2

holder.progressBar.setProgress(Integer.parseInt(total+""));此行是给你一个错误。您试图将“1.0”字符串解析为Integer,但值1.0是浮点类型。

请试试这样: 取浮动值并舍入它。这将返回整数

Math.round(Float.parseFloat(total+""))

2

只要改变:

Integer.parseInt(total+"") 

到:

(int) total 

,或者,如果你想更精确:

Math.round(total) 
+0

他不能直接将字符串转换为int,因为总数是字符串值 –

+0

因为+“” –

+0

我认为它是浮点数或双精度值它实际上是,因为您可以看到total = budget - used,是双打的 –

1

你哈已经替换: holder.progressBar.setProgress(Integer.parseInt(total+""));

随着:

holder.progressBar.setProgress(Math.round(total));

0

total要么是doublefloat,所述setProgress()方法采用int作为参数。

所以,你可以做到以下几点,改变total类型的int

或使用:

setProgress(Math.round(total)); 

Math.round功能可以通过doublefloat使用,它将再现int如果您通过float,或通过double通过long。输出将返回将输入四舍五入到最接近的十进制数的值。