从数据库值中设置listview中每个项目的进度条值

时间:2017-01-02 11:31:18

标签: android listview

我创建了自定义列表视图,我有进度条。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/selektor"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:paddingLeft="10dp">


<TextView
        android:id = "@+id/l_cat"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"
        android:text = "NAME"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textSize="20sp" />

    <ProgressBar
        style="@style/CustomProgressBarHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar4"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp" />
</RelativeLayout>

我想使用ViewBinder设置进度条最大值和进度条进度,所以我这样做了:

private class CustomViewBinder implements SimpleCursorAdapter.ViewBinder{
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if(view.getId()==R.id.progressBar4)
            {
                ProgressBar progress = (ProgressBar) view;
                    progress.setMax(cursor.getColumnIndex("MAX"));
                    progress.setProgress(cursor.getColumnIndex("PROGRESS"));
                return true;
            }

            return false;
        }

    }

SimpleCursorAdapter的代码:

String[] from = new String[] {category.KEY_NAME};
int[] to = new int[] {R.id.l_category};


adapter = new SimpleCursorAdapter(this,R.layout.customListView,cursor,from,to,0);
adapter.setViewBinder(new CustomViewBinder());
listCategories.setAdapter(adapter);

这段代码有什么问题?它没有设置我的进度条值。

2 个答案:

答案 0 :(得分:0)

你没有从光标中获取值!使用getString()获取索引的值。如果将MAX设置为double / int(例如getIntegargetDouble

,则应更改为获取数据类型
progress.setMax(Integer.parseInt(cursor.getString(cursor.getColumnIndex("MAX"))));
progress.setProgress(Integer.parseInt(cursor.getString(cursor.getColumnIndex("PROGRESS"))));

答案 1 :(得分:0)

我发现了我的错误。这很容易,但我不确定这是否是设置它的好习惯。

所以,我做的是,我添加到我的int[] to我的进度条ID,因为ViewBinder正在查找int[]内的视图。我不知道在from[]内放什么,所以我把数据库中的随机列索引。

工作代码:

String[] from = new String[] {category.KEY_SS,category.KEY_NAME};
int[] to = new int[] {R.id.progressBar4,R.id.l_category};


adapter = new SimpleCursorAdapter(this,R.layout.customListView,cursor,from,to,0);
adapter.setViewBinder(new CustomViewBinder());
listCategories.setAdapter(adapter);

private class CustomViewBinder implements SimpleCursorAdapter.ViewBinder{
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if(view.getId()==R.id.progressBar4)
            {
                ProgressBar progress = (ProgressBar) view;
                    progress.setMax(cursor.getInt(cursor.getColumnIndex("MAX")));
                    progress.setProgress(cursor.getInt(cursor.getColumnIndex("PROGRESS")));
                return true;
            }

            return false;
        }

    }

谢谢大家的帮助。