4

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.

user
  • 86,916
  • 18
  • 197
  • 190
user1324936
  • 2,187
  • 4
  • 36
  • 49
  • use view.findViewById(android.R.id.text1) and view.findViewById(android.R.id.progress) – Renard May 01 '12 at 10:23
  • ProgressBar progressBar = (ProgressBar)view.findViewById(R.id.progressBar1); gives me only a null pointer. btw. what is the argument to in the simpleAdapter constructor good for? can i use it? and how to get data string progress? – user1324936 May 01 '12 at 10:48
  • I was wrong. SimpleAdapter only works with TextViews. So you need to use a custom adapter. E.g: extend ArrayAdapter and override getView(). There you can use findViewById. – Renard May 01 '12 at 10:53
  • are you sure? thats why i use the Viewbinder. Android documentation says: This class can be used by external clients of SimpleAdapter to bind values to views. You should use this class to bind values to views that are not directly supported by SimpleAdapter or to change the way binding occurs for views supported by SimpleAdapter. – user1324936 May 01 '12 at 11:07

1 Answers1

9

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;
}
user
  • 86,916
  • 18
  • 197
  • 190
  • 1
    thats it thanks! but how to set progressBar.setProgress(); with the information of Object data? getData(path) returns a List. protected List> getData(String prefix) { List> myData = new ArrayList>(); its filled with protected void addItem(List> data, String name, Intent intent, int progress) { Map temp = new HashMap(); temp.put("title", name); temp.put("progress", name); temp.put("intent", intent); data.add(temp); – user1324936 May 01 '12 at 13:06
  • @user Thank you for explaining this. The documentation is very vague about what this "data" argument consists of. Now I understand. – LarsH Nov 01 '22 at 11:09