简体   繁体   中英

Seekbar Custom Adaptor - get values of Seekbar from OnItemClickListener

I have an array of categories which I have stores as an array of strings. I want to use a seekbar to set an int value which will be associated with each individual category. Given the category array will change size I have used a custom adaptor to allow for this. My custom adaptor takes each string in my categories array and creates a textview which it populates with the category name and creates a seekbar which the user is to interact with to set a value to be associated with the category name. I want to then take all the values of the seekbars and store them in an array which will be associated with the categories array.

I do not know how to get the seekbar values given they are programmatically created in a custom array. I have tried to use "getItemAtPosition()" but this will return the category name not the seekbar value.

Could someone tell me an approach I should take to somehow get the values please. Images attached: 1 custom adaptor code extract. 2 image of how custom adaptor intended to be used.

Thank you

Ken

自定义适配器代码

打算如何使用自定义适配器

The way I learnt to do this in the end was to make my custom adapter an inner class of my activity. By doing this I had global variables I could send data between by adapter and the activity it populates.

Regarding getting the progress of the seekbar: The adapter doesnt just work to create the views, it is ongoing in the sense that if you interact with any of its views it created it registers it. Use this to set an on seekbar change listener in which you get the progress of the seekbar being changed. Pass this to a global variable and then pass this to your main activity to be used for whatever.

I found this tutorial useful: Although it uses a recycler view (which is better to do anyway). http://vikramvalluvan.blogspot.co.uk/2016/01/interactive-listview-using-recyclerview.html?m=1

Snippet of my custom adapter class below. Note I have a global variable of "progressValue" set outside the snippet below.

class CustomAdaptor_InputSliders extends ArrayAdapter {

    public CustomAdaptor_InputSliders(Context context, String[] resource) {
        super(context, R.layout.input_slider_01, resource);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater Inflater = LayoutInflater.from(getContext());
        View customView = Inflater.inflate(R.layout.input_slider_01, parent, false);

        String singleViewItem = getItem(position);
        TextView title = (TextView) customView.findViewById(R.id.textView5);
        SeekBar seekbar = (SeekBar) customView.findViewById(R.id.seekBar2);

        title.setText(singleViewItem);


        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {


            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {


            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar bar) {
                progressValue = bar.getProgress();

            }
        });


        return customView;
    }


}

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