如何使用适配器更新List活动中的ProgressBar

时间:2012-05-21 09:46:21

标签: android progress-bar listactivity

public class ChallengesActivity extends ListActivity {

    private SimpleAdapter adapter;
    private ArrayList<HashMap<String,String>> list =  new ArrayList<HashMap<String,String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        printTripsToScreen(); 
        setContentView(R.layout.challenge_list);
        adapter = new SimpleAdapter(this, list, R.layout.challenge_row, 
                new String[] {"description","progress"},
                new int[] {R.id.ch_description, R.id.ch_progress});
        setListAdapter(adapter);
    }

    public void printTripsToScreen() {
           Log.d("Carbon","ChallengesActivity: printTripsToScreen() started");
           ChallengeManager chManager = new ChallengeManager();
           ArrayList<Challenge> challenges = chManager.getChallenges();
           HashMap<String,String> temp;

           for (int i=0; i<challenges.size();i++){
               temp = new HashMap<String,String>();
               temp.put("description","Hello");
               temp.put("progress", ""+challenges.get(i).getCompleted_percentage());
               list.add(temp);
           }
       }
}

我需要更新进度条中的值。我正在使用适配器和HashMap来更新文本内容。如何更新进度条中的值?如果不是这样,请告诉我怎么做?该清单是动态的。

1 个答案:

答案 0 :(得分:1)

如果Progressbar行中有ListView并且您想要更新其进度(使用您已有的值),则可以使用SimpleAdapter.ViewBinder来绑定值那个ProgressBar。 Bellow是一个示例,当您点击ProgressBar行时,会将0(从该行)重置为ListView

    //...
    //I assumed that you have a ProgressBar in the ListView row with the id progressBar1
    // and you use the the value from the "progress" key as its progress.
    adapter = new SimpleAdapter(this, list,
                    R.layout.someclasses_challaenge_row, new String[] {
                            "description", "progress", "progress"}, new int[] {
                            R.id.ch_description, R.id.ch_progress, R.id.progressBar1});
            adapter.setViewBinder(new SimpleAdapter.ViewBinder() {

                @Override
                public boolean setViewValue(View view, Object data,
                        String textRepresentation) {
                    if (view.getId() == R.id.progressBar1) {
                        int value = Integer.parseInt(data.toString());
                        ((ProgressBar) view).setProgress(value);
                        return true;
                    }
                    return false;
                }
            });
            setListAdapter(adapter);

    @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            HashMap<String, String> item = list.get(position);
            item.put("progress", "0");
            adapter.notifyDataSetChanged();
    }

我不知道这是不是你想要的。