0

I created custom listview, where I have progressbar.

<?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>

I want to set progressbar max value and progressbar progress using ViewBinder, so I did this:

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;
        }

    }

And code for 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);

What is wrong with this code? It doesn't set my progressbar values.

ktos1234
  • 197
  • 1
  • 6
  • 22
  • `from` and `to` arrays have only one element so only one `TextView` is mapped by your adapter – pskink Jan 02 '17 at 15:19

2 Answers2

0

You aren't getting the value from cursor! use getString() to get the value of the index. you should change to get data type if set your MAX to double/int like getIntegar or getDouble

progress.setMax(Integer.parseInt(cursor.getString(cursor.getColumnIndex("MAX"))));
progress.setProgress(Integer.parseInt(cursor.getString(cursor.getColumnIndex("PROGRESS"))));
Sadiq Md Asif
  • 882
  • 6
  • 18
  • 1
    I think setMax require int value not String – Linh Jan 02 '17 at 11:41
  • no, Cursor#getInt won't work if the data type in the database column is set to string. it should be this way Cursor#getType = columnType – Sadiq Md Asif Jan 02 '17 at 11:49
  • I tried your code and it still doesn't work. I also tried to set my own values: `progress.setMax(100)` , `progress.setProgress(50)` and it doesn't even work. – ktos1234 Jan 02 '17 at 11:52
  • no, the OP knows better what column types he has, if the column "PROGRESS" is mapped to ProgressBar's `progress` attribute you should use `getInt`, not `getString` followed by `Integer.parseIn` – pskink Jan 02 '17 at 11:53
  • @ktos1234 did you see my comment? i already answered you twice why your `ViewBinder` is not called – pskink Jan 02 '17 at 11:54
  • I saw your comment. I tried cursor.getInt and it didn't help. Isn't ViewBinder called here: adapter.setViewBinder(new CustomViewBinder()); ? – ktos1234 Jan 02 '17 at 12:06
  • I don't get it how to use it in my code. Can you write an answer? I can give more details. I wrote my code following this answer http://stackoverflow.com/questions/10396345/how-to-use-simpleadapter-viewbinder and there's no loop in that code. – ktos1234 Jan 02 '17 at 14:45
  • use log or debug to check if these lines are returning any values or not. Log.d("TAG",String.valueOf(cursor.getString(cursor.getColumnIndex("MAX"))); – Sadiq Md Asif Jan 02 '17 at 17:37
  • I run app in debug mode, and it seems like it doesn't even go inside this if: `if (view.getId() = R.id.progressBar4){}` I put Log.d after IF, and it returned values: `D/TAG: 27 D/TAG: 14 D/TAG: 27 D/TAG: 14 D/TAG: 27 D/TAG: 14` It's weird because It supposed to return 44 (max for first column), 15 (progress for first column) and 27 (max for second column) , 14(progress for second column ). I don't know why it returned only values from second column. – ktos1234 Jan 03 '17 at 16:31
  • Any ideas someone? IF statemets is wrong because it never goes inside if statement. – ktos1234 Jan 05 '17 at 18:42
0

I found where I had a mistake. It was so easy, but I am not sure if this is good practice for setting this up.

So, what I did is, I added to my int[] to my progressbar ID, because ViewBinder is looking for views what are inside int[]. I didn't know what to put inside from[] so I put random column index from my database.

Working code:

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;
        }

    }

Thank you all for help.

ktos1234
  • 197
  • 1
  • 6
  • 22