在活動中,它有一個ListView
其中ListView
項目由3 TextView
(monthAndYear,totalUsed(RM20)和預算(RM123.0)),和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.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)
他不能直接將字符串轉換爲int,因爲總數是字符串值 –
因爲+「」 –
我認爲它是浮點數或雙精度值它實際上是,因爲您可以看到total = budget - used,是雙打的 –