2
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 来更新文本内容。如何更新进度条中的值?如果这不是方法,请让我知道该怎么做?该列表是动态的。

4

1 回答 1

1

如果您Progressbar在 的行中有 aListView并且您想更新其进度(使用您已经拥有的值),您可以使用 aSimpleAdapter.ViewBinder来绑定 that 的值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();
    }

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

于 2012-05-21T10:40:26.213 回答