How to use SimpleAdapter.ViewBinder?

不想你离开。 提交于 2020-01-29 09:43:45

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/10396345/how-to-use-simpleadapter-viewbinder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!