How to use SimpleAdapter.ViewBinder?

user1324936 picture user1324936 · May 1, 2012 · Viewed 7k times · Source

I have a list with a complex layout R.layout.menu_row. It consists of a ProgressBar and a text field. The adapter I use:

   SimpleAdapter simpleAdapter = new SimpleAdapter(this, getData(path),
            R.layout.menu_row, new String[] { "title", "progress" },
            new int[] { R.id.text1,R.id.progressBar1});

The adapter knows how to handle TextViews by it self but not ProgressBars, so I wrote a complex data binder:

    SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Object data, String textRepresentation) {
            //here goes the code
            if () {
                return true;
            }
            return false;
        }

Now I'm stuck filling the mapping inside the function. I need to set the value of string progress to the setProgress method of the ProgressBar. But I don't get a handle to string progress and to the ProgressBar.

Answer

user picture user · May 1, 2012

You need to find out if the ViewBinder is called for the ProgressBar and set its progress(from the data parameter(the data from column progress in your case)):

SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Object data, String textRepresentation) {
            if (view.getId() == R.id.progressBar1) {
                // we are dealing with the ProgressBar so set the progress and return true(to let the adapter know you binded the data)
                // set the progress(the data parameter, I don't know what you actually store in the progress column(integer, string etc)).                             
                return true;
            }
            return false; // we are dealing with the TextView so return false and let the adapter bind the data
}

EDIT : I've seen in your addItem method that you do:

temp.put("progress", name);// Why do you set again the name as progress?!?

I think what you should set here is the progress parameter:

temp.put("progress", progress);

Then in the ViewBinder:

if (view.getId() == R.id.progressBar1) {
   Integer theProgress = (Integer) data;
   ((ProgressBar)view).setProgress(theProgress); 
   return true;
}