简体   繁体   中英

Get and Set an ArrayList<String> inside an object to populate Spinner

What would the syntax be if I had an object where I wanted to set an ArrayList? Here is a sample Object with getter and setter (multiple other Strings and ints are set inside Some_obj that have been removed for this example):

public class Some_obj {

    protected ArrayList<String> spinnerList = new ArrayList<String>();

    public ArrayList<String> getList() { return spinnerList; }
    public void setList(ArrayList<String> spinnerList) { this.spinnerList = spinnerList; }

    public Some_obj(ArrayList<String> _sl) { spinnerList = _sl; }
}

How would I declare it inside the object, since this current code is basically setting a new list any time it need to set the adapter for a spinner. Do I need to declare list like this?

protected ArrayList<String> spinnerList;

so I can do something like this?

Some_obj obj = listViewCells.get(selectedPosition); 
obj.setList(listFromActivity);

I'm currently able to populate the spinner successfully with listFromActivity, however I see it beneficial to store the listFromActivity inside Some_obj in the near future...it just seems like I can't figure out how to set an ArrayList inside Some_obj if it's first set as null inside Some_obj.

Hopefully this can help someone else, even though I was setting the list correctly. I need to override the natural spinner touch that pops the selections, so I employed the spinnerOnTouch listener inside the adapter, where "holder" is a the class that holds the cell's contents. So I set the listener inside the adapter's getView() like:

holder.spinner.setOnTouchListener(spinnerOnTouch);

Without getting into too much detail about what I fixed over the past hour, here is the working code that is pulling the correct obj.list for a spinner that's already populated it's selections:

private View.OnTouchListener spinnerOnTouch = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        selectedPosition = Integer.parseInt(v.getTag().toString());
        Some_obj obj = cells.get(selectedPosition);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            adapter = null;
            if(obj.list == null) {
                adapter = new ArrayAdapter<String>(mContext, R.layout.spinner_cell, listFromActivity);
                spinner = (Spinner)v.findViewById(R.id.spinner);
                spinner.setAdapter(adapter);
                new GetSelectionsTask().execute();
            }
            else
            {
                adapter = new ArrayAdapter<String>(mContext, R.layout.spinner_cell, obj.list);
                spinner = (Spinner)v.findViewById(R.id.spinner);
                spinner.setAdapter(adapter);
            }
        }
        return false;
    }
};

Long story short, you should avoid trying to add spinners inside ListViews, because you'll have to do quite a bit of work to keep your position in the adapter's getView(), the spinner's selection, and updating the objects associated with each list...not to mention the conditions when the spinner appears, because the getView() method will change it's position to 0 no matter what cell you're working with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM