将进度设置为进度条

时间:2016-12-30 08:16:03

标签: android listview progress-bar number-formatting

在活动中,它有一个ListView,其中ListView项目包含3个TextView(monthAndYear,totalUsed(RM20)和预算(RM123.0))和一个{ {1}}。我尝试使用以下代码来显示ProgressBar中的进度,但没有任何反应。

enter image description here

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());

4 个答案:

答案 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)

totaldoublefloatsetProgress()方法以int为参数。

因此,您可以执行以下操作,将total的类型更改为int

或使用:

setProgress(Math.round(total));

Math.round功能可以与doublefloat一起使用,如果您将int传递给float,它会重现long,或者double如果您将has_many :works, dependent: :destroy def performers works.map {|x| x.performer} end 传递给它。输出将返回将输入四舍五入的值。