2016-12-30 244 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。輸出將返回將輸入四捨五入到最接近的十進制數的值。