开发者

Redraw android spinner

开发者 https://www.devze.com 2023-02-11 04:09 出处:网络
I have an android spinner item, in which the lines are made up of a TextView and an ImageView (a star, to mark that the user has marked that particular item as a favourite). Now, when a user favourite

I have an android spinner item, in which the lines are made up of a TextView and an ImageView (a star, to mark that the user has marked that particular item as a favourite). Now, when a user favourites an item, I want to make the star appear next to that item in the spinner list, so I want to redraw the spinner to show the updated images. How do I do this?

What I'm doing now is that I'm creating a whole new spinner, with the appropriate images. This makes my spinner set the selected item to the first item in the list, which is annoying - I want it to stay the same. I have tried

spinner.setSelection(selectedItem);

This works for ANY number other than the number I actually want (the position of the currently selected item), then it sets the selected spinner item to the first in the list again.

So: how can I redraw the list with the updated information without having to recreate the whole list, or alternatively, how can I recreate the list and still preserve the spinner item selection?

To clarify: here's what I'm doing

// Callback for "favourite"-button
star.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Toggle favourite
            getActiveSelection().setStarred(getActiveSelection().isStarred() ? false : true);
            reloadText();
            createSpinner();
        }
    });

 private void createSpinner() {

    spinner = ((Spinner)findViewById(R.id.spinner));

    ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<开发者_如何学CString, Object>();

    // Populate spinner item list
    for(int i=0; i<N; i++) {
        SpinnerItem item = getSpinnerItem(i);
        map = new HashMap<String, Object>();
        map.put("Name", item.getName());
        map.put("Icon", (item.isStarred() ? "On" : "Off"));
        list.add(map);
    }        

    MySpinnerAdapter aspn = new MySpinnerAdapter(this, list,
            R.layout.spinner, new String[] { "Name", "Icon" },
            new int[] { R.id.spinnerrow, R.id.spinnerrowicon });


    spinner.setAdapter(aspn);
    spinner.setSelection(getActiveSelection().getSpinnerPosition());
}


You may just update spinner adapter - let it know which item is starred when user clicks on the star icon (star.setOnClickListener...). What I would do:

  1. In adapter construction, you pass only list of "Name" fields, no need for icon (all not starred by default)
  2. You add separate function to adapter to set starred item (setStarred(String name))
  3. On initial call, invoke setStarred to set initial selection (if any)
  4. in star.setOnClickListener implementation, call setStarred for starred item on adapter which you obtain as (MySpinnerAdapter)spinner.getAdapter()


Or just try:

spinner.setSelection(getActiveSelection().getSpinnerPosition(),true);

That worked for me to update the spinner...


You need to call notifyDataSetChanged() on your spinner adapter when your data has changed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号